Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing string to local date doesn't use desired century

I am using this DateTimeFormatter:

DateTimeFormatter.ofPattern("ddMMYY")

I want to parse the string 150790 and I got this error:

Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=15, MonthOfYear=7, WeekBasedYear[WeekFields[MONDAY,4]]=2090},ISO of type java.time.format.Parsed

Obviously, I want to get the following TemporalAccessor:

{DayOfMonth=15, MonthOfYear=7, WeekBasedYear=1990}

Do you know why I got the year 2090 instead of 1990?

Thanks for your help

like image 229
Happy Avatar asked Apr 07 '15 11:04

Happy


1 Answers

Since this question is really about new java.time-package and NOT SimpleDateFormat I will cite following relevant section:

Year: The count of letters determines the minimum field width below which padding is used. If the count of letters is two, then a reduced two digit form is used. For printing, this outputs the rightmost two digits. For parsing, this will parse using the base value of 2000, resulting in a year within the range 2000 to 2099 inclusive.

We see that Java-8 uses the range 2000-2099 per default, not like SimpleDateFormat the range -80 years until +20 years relative to today.

If you want to configure it then you have to use appendValueReduced(). This is designed in an inconvenient way, but possible, see here:

String s = "150790";

// old code with base range 2000-2099
DateTimeFormatter dtf1 = 
  new DateTimeFormatterBuilder().appendPattern("ddMMyy").toFormatter();
System.out.println(dtf1.parse(s)); // 2090-07-15

// improved code with base range 1935-2034
DateTimeFormatter dtf2 = 
  new DateTimeFormatterBuilder().appendPattern("ddMM")
  .appendValueReduced(
    ChronoField.YEAR, 2, 2, Year.now().getValue() - 80
  ).toFormatter();
System.out.println(dtf2.parse(s)); // 1990-07-15

By the way, if you really want week-based years then you have to use Y instead of y or the appropriate field IsoFields.WEEK_BASED_YEAR. Regarding the fact that you don't have any other week-related fields I would assume the normal calendar year, not the week-based one.

like image 183
Meno Hochschild Avatar answered Sep 20 '22 05:09

Meno Hochschild