Parsing string to local date doesn't use desired century

丶灬走出姿态 提交于 2019-12-17 05:12:32

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/29490893/parsing-string-to-local-date-doesnt-use-desired-century

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!