在java中验证日期

use*_*746 4 java validation datetime

我正在使用NetBeans IDE 7.2.我有两个单独的类newDateTest.javanewDateMethod.java,我目前使用我的方法类验证从我在我的测试类已经使用的用户输入的日期.

到目前为止,在我的Test课程中,我有以下内容:

try
{
    Prompt ="please enter a date in the format dd-mm-yyyy";
    System.out.println(Prompt);
    String inputDate = in.next();
    isValid = newDateMethod.validDate(input, input, input);
    if (isValid){
        System.out.println("VALID DATE");

    } else {
        System.out.println("INVALID DATE");

    }

}catch (ArrayIndexOutOfBoundsException oob) {
    System.out.println(oob);
}
}
Run Code Online (Sandbox Code Playgroud)

但是我不知道如何在我的方法类中验证日期,因为我对Java很新.

任何人都可以找到解决方案吗?我被教导使用的那种东西是Date Formmater,但不确定这是否合适?如果是这样就不知道如何使用它

duf*_*ymo 11

像这样:

Date date = null;
String inputDate = "07-01-2013";
try {
    DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
    formatter.setLenient(false);
    date = formatter.parse(inputDate);
} catch (ParseException e) { 
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)