参考资料
⏹在Java8的新日期API下,yyyy
代表公元纪年(year-of-era),而uuuu
代表和公元没有关系的年
⏹使用java8的日期API进行开发的时候,大多数使用的都是uuuu
。
⏹字符串转换为LocalDate等时间的时候,建议开启严格模式。
以2023/02/29
为例子,2023年的2月份不可能有29天。
⏹当DateTimeFormatter.ofPattern()
进行非严格模式下的格式化的时候,
yyyy/MM/dd
和uuuu/MM/dd
表现相同,都是转换为合法的日期。
String str1 = "2023/02/29";
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate1 = LocalDate.parse(str1, formatter1);
System.out.println(localDate1); // 2023-02-28
DateTimeFormatter formatter1_1 = DateTimeFormatter.ofPattern("uuuu/MM/dd");
LocalDate localDate1_1 = LocalDate.parse(str1, formatter1_1);
System.out.println(localDate1_1); // 2023-02-28
DateTimeFormatter.ofPattern()
通过.withResolverStyle(ResolverStyle.STRICT)
来开启严格模式。⏹虽然2023/02/28
是一个合法的日期,但是yyyy
代表公元纪年,且开启了严格模式,2023/02/28
并不包含公元,因此程序报错。
String str2 = "2023/02/28";
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy/MM/dd")
.withResolverStyle(ResolverStyle.STRICT);
try {
LocalDate localDate2 = LocalDate.parse(str2, formatter2);
System.out.println(localDate2);
} catch (Exception ex) {
System.out.println(ex.getMessage());
// Text '2023/02/28' could not be parsed:
// Unable to obtain LocalDate from TemporalAccessor:
// {DayOfMonth=28, YearOfEra=2023, MonthOfYear=2},ISO of type java.time.format.Parsed
}
⏹这次使用G yyyy/MM/dd
来进行格式化,并且日期也添加了西暦
,因此在严格模式下也能格式化成功。
String str3 = "西暦 2023/02/28";
/*
* Locale.JAPAN --> "西暦 2023/02/28"
* Locale.CHINA --> "公元 2023/02/28"
* Locale.US --> "AD 2023/02/28"
*/
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("G yyyy/MM/dd", Locale.JAPAN)
.withResolverStyle(ResolverStyle.STRICT);
LocalDate localDate3 = LocalDate.parse(str3, formatter3);
System.out.println(localDate3); // 2023-02-28
⏹因为开启了严格模式下的日期格式化,因此2023/02/29
这种不存在的日期会异常。
// 严格模式下的日期格式化
DateTimeFormatter formatter4 = DateTimeFormatter.ofPattern("uuuu/MM/dd")
.withResolverStyle(ResolverStyle.STRICT);
try {
LocalDate.parse("2023/02/29", formatter4);
} catch (Exception ex) {
System.out.println(ex.getMessage());
// Text '2023/02/29' could not be parsed: Invalid date 'February 29' as '2023' is not a leap year
}
LocalDate localDate4 = LocalDate.parse("2023/02/28", formatter4);
System.out.println(localDate4); // 2023-02-28
⏹若想让日期格式化之后含日本年号,则应使用G yy/MM/dd
。
import java.time.chrono.JapaneseDate;
JapaneseDate japaneseDate1 = JapaneseDate.of(2017, 10, 22);
String japaneseDate_str_1 = japaneseDate1.format(DateTimeFormatter.ofPattern("G uuuu/MM/dd", Locale.JAPAN));
System.out.println(japaneseDate_str_1); // 平成 2017/10/22
JapaneseDate japaneseDate2 = JapaneseDate.from(LocalDate.now());
String japaneseDate_str_2 = japaneseDate2.format(DateTimeFormatter.ofPattern("G yyyy/MM/dd", Locale.JAPAN));
System.out.println(japaneseDate_str_2); // 平成 0035/09/27