简体   繁体   中英

Java: How to parse a date strictly?

SimpleDateFormat is a very kind parser that rolls the resulting date instead of throwing an error. How can I parse a date strictly without regexps etc?

fmt = new SimpleDateFormat("dd.MM.yyyy")
fmt.parse("10.11.2012")   // it works
fmt.parse("10.1150.2012") // it works but it's unwanted

fmt.setLenient(false); is what you're looking for.

java.time

You can also use the java.time package in Java 8 and later ( Tutorial ). Its parsing strictly checks the date values.

For example:

String strDate = "20091504";
TemporalAccessor ta = DateTimeFormatter.ofPattern("yyyyMMdd").parse(strDate);

Gives directly:

Exception in thread "main" java.time.format.DateTimeParseException:
Text '20091504' could not be parsed:
Invalid value for MonthOfYear (valid values 1 - 12): 15

Unfortunately fmt.setLenient(false); will not achieve strict date formatting. It helps some, for example parsing "2010-09-01" using format "yyyyMMdd" will succeed if lenient==true, but the result is very bizarre: 2009/12/09.

Even if lenient==false parse() still acts lenient. "2010/01/5" is allowed for the pattern "yyyy/MM/dd". And data disagreement like "1999/2011" for the pattern "yyyy/yyyy" is tolerated (yielding 2011). Garbage is also allowed after the pattern match. For example: "20100901" and "20100901andGarbage" will both match "yyyyMMdd".

I have written an extension of SimpleDateFormat that enforces strict pattern matching. You can find it here:

SimpleDateFormat.parse() ignores the number of characters in pattern

In my version format() and parse() are functional inverses. This is what I think most people would expect.

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