With Java 8, the code below parses "18" into year "0018" instead of "2018".
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/y");
return LocalDate.parse(date, formatter);
input date is "01/05/18".
1) why the result is "0018"? Does DateTimeFormatter
not follow the 80-20 rule?
2) How to control SimpleDateFormat parse to 19xx or 20xx? talked about SimpleDateFormat.set2DigitYearStart(Date)
can be used to fix the year. Is there something similar to that for DateTimeFormatter
?
I was hoping "M/d/y" will parse both 2 and 4 digit years.
"M/d/yy" throws Exception for 4 digit years and parses "01/05/97" to "2097-01-05". Ideally this should be parsed to "1997-01-05".
"M/d/yyyy" throws Exception for 2 digit years.
M/d/yy
, as you want to support a two-digit year? – Idiophoneu
rather thany
and got same behavior. Example: input "1/2/8" with pattern "M/d/u" =0008-01-02
. Seems only the double-digituu
oryy
engages the default century of2000
. – Hulking