由于这个问题实际上是关于新的java.time-package而不是SimpleDateFormat,我将引用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.
我们看到Java-8使用默认值2000-2099的范围,而不是像SimpleDateFormat那样,相对于今天的范围为-80年,直到20年.
如果你想配置它,那么你必须使用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.关于您没有任何其他与周相关的字段的事实,我会假设正常的日历年,而不是基于周的日历年.