将字符串解析为本地日期不会使用所需的世纪

我正在使用这个DateTimeFormatter:

DateTimeFormatter.ofPattern("ddMMYY") 

我想解析字符串150790 ,我收到此错误:

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

显然,我想获得以下TemporalAccessor

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

你知道我为什么得到2090而不是1990年?

谢谢你的帮助

由于这个问题实际上是关于新的java.time而不是SimpleDateFormat我将引用以下相关部分 :

年份:字母数决定了使用填充的最小字段宽度。 如果字母数是2,则使用减少的两位数forms。 对于打印,这将输出最右边的两位数字。 对于解析,这将使用2000的基值进行解析,从而产生2000到2099(包括2000和2099)范围内的一年。

我们看到Java-8使用默认2000-2099的范围,而不是像SimpleDateFormat的范围 – 相对于今天+80年的范围。

如果要配置它,则必须使用appendValueReduced() 。 这是一个不方便的设计,但可能,请看这里:

 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 

顺便说一句,如果你真的想要基于周的年份,那么你必须使用Y而不是y或相应的字段IsoFields.WEEK_BASED_YEAR 。 关于您没有任何其他与周相关的字段这一事实,我会假设正常的日历年,而不是基于周的日历年。