简体   繁体   中英

SimpleDateFormat fails to reject input missing century on the year of the input, despite “yyyy” in the formatting pattern

I have a SimpleDateFormat with the pattern yyyy-Md" , and the following scenario:

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

The output is Sat Mar 04 00:00:00 EST 2

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

Well, I can explain it from the docs :

For parsing, if the number of pattern letters is more than 2, the year is interpreted literally, regardless of the number of digits. So using the pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 AD

It's possible that Joda Time would be stricter - and it's a better API in general, IMO...

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

tl;dr

Using java.time , that input with that formatting pattern fails, just as you expected.

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

…throws java.time.format.DateTimeParseException

java.time

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

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

Define a custom formatting pattern as you asked. Use the DateTimeFormatter class.

DateTimeFormatter f = DateTimeFormatter.ofPattern ( "uuuu-M-d" );

Try to parse your input.

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

We encounter a DateTimeParseException , failing on the missing century of the year of the input. Just as you expected.

Exception in thread "main" java.time.format.DateTimeParseException: Text '02-03-04' could not be parsed at index 0


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 .

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

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

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

Where to obtain the java.time classes?

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 .

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