简体   繁体   中英

Does the java.util.Date object verifies the date validity?

I just wrote this unit tests :

@Test
public void testGetDateFromString() throws ParseException{
    String date = "52/29/2500";
    Date dateFromString = DateHelper.getDateFromString(date, DateHelper.DD_MM_YYYY_FORMAT);

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateHelper.DD_MM_YYYY_FORMAT);
    Date dateWithSimpleFormat = simpleDateFormat.parse(date);

    Assert.assertNotNull(dateFromString);
    Assert.assertNotNull(dateWithSimpleFormat);

    Assert.assertTrue(dateFromString.equals(dateWithSimpleFormat));

    System.out.println("dateFromString " + dateFromString);
    System.out.println("dateWithSimpleFormat " + dateWithSimpleFormat);
}

And the output is :

dateFromString Wed Jun 21 00:00:00 CEST 2502
dateWithSimpleFormat Wed Jun 21 00:00:00 CEST 2502

The DateHelper.DD_MM_YYYY_FORMAT pattern is dd/MM/yyyy and getDateFromString is a method that parses a String date to a Date object using commons-lang library.

Why des the java.util.Date object verifies the date validity?

You need to set simpleDateFormat.setLenient(false); to make the SimpleDateFormat to validate your input strictly.

You can refer the setLenient documentation for further understanding. By the definition,

Specify whether or not date/time parsing is to be lenient. With lenient parsing, 
the parser may use heuristics to interpret inputs that do not precisely match this 
object's format. With strict parsing, inputs must match this object's format.

Use simpleDateFormat.setLenient(false); to enable strict parsing.

java.time

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu");

    String date = "52/29/2500";
    try {
        LocalDate dateWithJavaTime = LocalDate.parse(date, dateFormatter);

        System.out.println("dateWithJavaTime " + dateWithJavaTime);
    } catch (DateTimeParseException dtpe) {
        System.out.println("Invalid date. " + dtpe);
    }

The output from this code is:

Invalid date. java.time.format.DateTimeParseException: Text '52/29/2500' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 29

Please enjoy not only that the validation works, but also the precision of the error message.

Other results:

  • For the string 52/11/2500 the result is “Invalid date. java.time.format.DateTimeParseException: Text '52/11/2500' could not be parsed: Invalid value for DayOfMonth (valid values 1 - 28/31): 52”.
  • For the string 29/02/2019 we get “dateWithJavaTime 2019-02-28”, which may surprise. To have this string rejected use

      DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu") .withResolverStyle(ResolverStyle.STRICT); 

    Now we get

    Invalid date. java.time.format.DateTimeParseException: Text '29/02/2019' could not be parsed: Invalid date 'February 29' as '2019' is not a leap year

    Again enjoy how precise the message is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM