Java: How to parse a date strictly?

2019-01-12 02:34发布

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

3条回答
贪生不怕死
2楼-- · 2019-01-12 02:48

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

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-12 03:08

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.

查看更多
SAY GOODBYE
4楼-- · 2019-01-12 03:09

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
查看更多
登录 后发表回答