JDK 1.8 LocalDate 只要月份和日期是12.31,年份就会自增
看文档后发现应该使用 uuuu 来进行格式化,这下就能保证输出 MIN 和 MAX 注解中的值了。
u year year 2004; 04
y year-of-era year 2004; 04
Y week-based-year year 1996; 96
System.out.println(LocalDate.MAX.format(DateTimeFormatter.ofPattern("YYYY-MM-dd")));
根据 LocalDate.MAX 的注解得知,最终结果应该是+999999999-12-31
但运行后发现结果为 +1000000000-12-31
经过多次尝试以后发现,只要月份是12,日期是31时,年份就会自增。
而且修改时区好像对结果也没有什么影响。
JDK 1.8.0_121
Win10
以供复制:
import java.time.LocalDate;import java.time.format.DateTimeFormatter;
import java.util.Date;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-dd");System.out.println(LocalDate.MAX.format(formatter));
LocalDate date = LocalDate.of(2017, 12, 31);
System.out.println(date.format(formatter));
date = LocalDate.of(2017, 12, 30);
System.out.println(date.format(formatter));
date = LocalDate.of(2018, 12, 31);
System.out.println(date.format(formatter));
System.out.println(new Date());
回答:
应该是你 日期格式选错了
// final String pattern = "YYYY-MM-dd"; final String pattern = "yyyy-MM-dd";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
System.out.println(LocalDate.MAX.format(formatter));
LocalDate date = LocalDate.of(2017, 12, 31);
System.out.println(date.format(formatter));
date = LocalDate.of(2017, 12, 30);
System.out.println(date.format(formatter));
date = LocalDate.of(2018, 12, 31);
System.out.println(date.format(formatter));
以上是 JDK 1.8 LocalDate 只要月份和日期是12.31,年份就会自增 的全部内容, 来源链接: utcz.com/p/181595.html