首先是4个日期如下:
String inDate1 = "1/1/2008";
String inDate2 = "10/20/2008";
String inDate3 = "100/20/2008";
String inDate4 = "01/10/2008";

明显"1/1/2008" "10/20/2008" "01/10/2008"的格式是正确的
"100/20/2008"的格式是错误的。

我写了如下的代码进行判断:
public class DateFormatTest {

    public static void main(String[] args) {
        String str1 = "MM/dd/yyyy";
        String inDate1 = "1/1/2008";
        String inDate2 = "10/20/2008";
        String inDate3 = "100/20/2008";
        String inDate4 = "01/10/2008";

        SimpleDateFormat date = new SimpleDateFormat(str1);
        try {
            if(inDate1.equals(date.format(date.parse(inDate1)))){
                System.out.println("The format of "+inDate1+" is correct.");
            }else{
                System.out.println("The format of "+inDate1+" is incorrect.");
            }
            if(inDate2.equals(date.format(date.parse(inDate2)))){
                System.out.println("The format of "+inDate2+" is correct.");
            }else{
                System.out.println("The format of "+inDate2+" is incorrect.");
            }
            if(inDate3.equals(date.format(date.parse(inDate3)))){
                System.out.println("The format of "+inDate3+" is correct.");
            }else{
                System.out.println("The format of "+inDate3+" is incorrect.");
            }
            if(inDate4.equals(date.format(date.parse(inDate4)))){
                System.out.println("The format of "+inDate4+" is correct.");
            }else{
                System.out.println("The format of "+inDate4+" is incorrect.");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }    
    }
}
结果是:
The format of 1/1/2008 is incorrect.
The format of 10/20/2008 is correct.
The format of 100/20/2008 is incorrect.
The format of 01/10/2008 is correct.

因为当formatter是"MM/dd/yyyy"的时候,系统会将"1/1/2008"转化为"01/01/2008",所以,在我上面的逻辑里,得到的结果是 "The format of 1/1/2008 is incorrect."

但是,如果我把formatter改成"M/d/yyyy",结果如下:
The format of 1/1/2008 is correct.
The format of 10/20/2008 is correct.
The format of 100/20/2008 is incorrect.
The format of 01/10/2008 is incorrect.
因为当formatter是"M/d/yyyy"的时候,系统会将"01/10/2008"转化为"1/10/2008",所以,在我上面的逻辑里,得到的结果是 "The format of 01/10/2008 is incorrect.

我现在想找到一个两全其美的方法能得到如下结果:
The format of 1/1/2008 is correct.
The format of 10/20/2008 is correct.
The format of 100/20/2008 is incorrect.
The format of 01/10/2008 is correct.

我知道可以用2个formatter来解决,但想问问高手们,有没有更好的办法,或者是有更好的类和方法来解决这个问题。