Java version: 1.8.0_202
see code below:
DateTimeFormatter yy = DateTimeFormatter.ofPattern("yy");
DateTimeFormatter yyy = new DateTimeFormatterBuilder()
.appendValueReduced(ChronoField.YEAR, 3, 3, 0)
.toFormatter();
DateTimeFormatter yyyy = DateTimeFormatter.ofPattern("yyyy");
LocalDateTime now = LocalDateTime.now();
// set year is 0
LocalDateTime year0 = now.withYear(0);
System.out.println("year0: " + year0); // year0: 0000-01-10T09:53:03.551
System.out.println("year0.getYear(): " + year0.getYear()); // year0.getYear(): 0
System.out.println("year0.format(yyyy): " + year0.format(yyyy)); // year0.format(yyyy): 0001
System.out.println("year0.format(yyy): " + year0.format(yyy)); // year0.format(yyy): 000
System.out.println("year0.format(yy): " + year0.format(yy)); // year0.format(yy): 01
System.out.println("============================================================");
// set year is 1
LocalDateTime year1 = now.withYear(1);
System.out.println("year1: " + year1); // year1: 0001-01-10T09:53:03.551
System.out.println("year1.getYear(): " + year1.getYear()); // year1.getYear(): 1
System.out.println("year1.format(yyyy): " + year1.format(yyyy)); // year1.format(yyyy): 0001
System.out.println("year1.format(yyy): " + year1.format(yyy)); // year1.format(yyy): 001
System.out.println("year1.format(yy): " + year1.format(yy)); // year1.format(yy): 01
为什么year0.format(yyyyy)
和year0.format(yyy)
0001或01
,但year0.format(yyyyyy)
返回权000
? 如何确定?