开发者

Java: Gregorian Calendar date validation issue

I’m trying to validate a date input by a user using the Gregorian Calendar in java (this is a must), however whenever I test a date in December it throws up the error below.

Exception in thread "main" java.lang.IllegalArgumentException: MONTH   
        at java.util.GregorianCalendar.computeTime(Unknown Source)  
        at java.util.Calendar.updateTime(Unknown Source)  
        at java.util.Calendar.getTimeInMillis(Unknown Source)  
        at java.util.Calendar.getTime(Unknown Source)

code below

public static boolean  ValidDate (int Day, int Month, int Year)  
    GregorianCalendar Date = new GregorianCalendar();  
    Date.setLenient(false);  
    Date.set(Year, Month, Day, 0, 0, 0);  
    try{  
        Date.getTime();  
        return true;  
        }catch (Exception e){  
            System.out.printl开发者_Go百科n("Date is invalid please try again");   
            return false;  
        }  
}

I haven’t been able to turn up anything relevant on google so any help would be awesome!


With the Calendar class, although days and years are numbered 1..n (one-based), months are numbered 0-11 (zero-based), so December is not month number 12, it is month number 11.

Try calling your method specifying 11 (instead of 12) for the month parameter.

This issue is just one of the many retarded aspects of the Calendar class.


Or, better yet, use the constant Calendar.DECEMBER. You don't have to worry about whether it's 11 or 12 under the covers.

Do yourself a favor: learn and follow the Java coding standards. I'd write it this way:

public static boolean  isvalidDate (int day, int month, int year)  
    Calendar calendar = Calendar.getInstance();
    calendar.setLenient(false);  
    calendar.set(year, month, day, 0, 0, 0);  
    try {  
        date.getTime();  
        return true;  
     } catch (Exception e) {  
        return false;  
     }  
 }


tl;dr

LocalDate.of( year , month , dayOfMonth )  // Catch exception `DateTimeParseException`

java.time

The modern approach uses the java.time classes rather than the troublesome old GregorianCalendar class.

The java.time.LocalDate class represents a date-only value without time-of-day and without time zone.

To validate your inputs, attempt to assemble a LocalDate from the numbers. Unlike GregorianCalendar, LocalDate has sane numbering of months, 1-12 for January-December. If the numbers result in an invalid date, a DateTimeParseException is thrown for you to catch.

try {
    LocalDate ld = LocalDate.of( year , month , dayOfMonth ) ;
    return true ;  // true = Valid data entered for a date.
} catch ( DateTimeParseException e ) {
    return false;  // Exception caught, meaning invalid data-entry.
}

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜