SimpleDateFormat cannot reject the entry of a missed century in the input year, despite the "yyyy" in the formatting template

I have a SimpleDateFormat with the yyyy-Md" template and the following script:

 String str = "02-03-04"; SimpleDateFormat f = new SimpleDateFormat("yyyy-Md"); f.setLenient(false); System.out.println(f.parse(str)); 

Sat Mar 04 00:00:00 EST 2 signal Sat Mar 04 00:00:00 EST 2

My goal was to catch dates only in a format like 2004-02-03, and ignore 02-03-04. I thought yyyy in the template would require a 4-digit year, but of course this is not the case. Can someone explain why this is not throwing a parse exception? I would like this ...

+5
source share
2 answers

Well, I can explain this from the docs :

For parsing, if the number of letters in the pattern exceeds 2, the year is interpreted literally, regardless of the number of digits. Thus, using the template "MM / dd / yyyy", "01/11/12" is analyzed until January 11, 12 AD.

It is possible that Joda Time will be more rigorous - and this is the best API overall, IMO ...

You can always throw an exception if the year is less than 1000 after parsing ...

+8
source

TL; dr

When using java.time, input with this formatting pattern fails, as you would expect.

 LocalDate .parse( "02-03-04" , DateTimeFormatter.ofPattern ( "uuuu-Md" ) ) 

... throws java.time.format.DateTimeParseException

java.time

The classes you use are now superseded by the modern java.time classes defined in JSR 310 and built into Java 8 and later.

The LocalDate class represents a value only for a date without a time of day and without a time zone or offset from UTC .

Define a custom formatting template as you requested. Use the DateTimeFormatter class.

 DateTimeFormatter f = DateTimeFormatter.ofPattern ( "uuuu-Md" ); 

Try to analyze your data.

 String input = "02-03-04"; LocalDate localDate = LocalDate.parse ( input , f ); 

We encountered a DateTimeParseException that failed in a missing century of input year. As you expected.

Exception in thread "main" java.time.format.DateTimeParseException: text '02 -03-04 'cannot be parsed with index 0


About java.time

The java.time framework is built into Java 8 and later. These classes supersede the problematic old obsolete date and time classes such as java.util.Date , Calendar and & SimpleDateFormat .

To learn more, check out the Oracle Tutorial . And look in Kara for many examples and explanations. Specification: JSR 310 .

The Joda-Time project, which is now in maintenance mode, recommends switching to java.time classes.

You can exchange java.time objects directly with your database. Use a JDBC driver compatible with JDBC 4.2 or later. No need for strings, no need for java.sql.* Classes.

Where to get java.time classes?

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

+1
source

All Articles