created: 2022-07-17T16:18:38+08:00
updated: 2022-07-17T17:36:18+08:00
tags:
- DateTimeAPI
- DateTimeAPI/DayOfWeek
- DateTimeAPI/Month
DayOfWeek and Month Enums
Date-Time API 提供了用于指定一周中的几天和一年中的月份的枚举。
DayOfWeek
DayOfWeek 常量的整数值范围从 1(星期一)到 7(星期日)。使用定义的常量 (DayOfWeek.FRIDAY) 使您的代码更具可读性。
这个枚举还提供了许多方法,类似于基于时间的类提供的方法。
System.out.printf("%s%n", DayOfWeek.MONDAY.plus(3));
getDisplayName(TextStyle, Locale)
DayOfWeek dow = DayOfWeek.MONDAY;
Locale locale = Locale.getDefault();
System.out.println(dow.getDisplayName(TextStyle.FULL, locale));
System.out.println(dow.getDisplayName(TextStyle.NARROW, locale));
System.out.println(dow.getDisplayName(TextStyle.SHORT, locale));
en本地输出
Monday
M
Mon
Month
Month 每个常量的整数值对应于从 1(一月)到 12(十二月)的 ISO 范围。使用定义的常量 (Month.SEPTEMBER) 使您的代码更具可读性。
Month 枚举还包括许多方法。
System.out.printf("%d%n", Month.FEBRUARY.maxLength());
getDisplayName(TextStyle, Locale)
Month month = Month.AUGUST;
Locale locale = Locale.getDefault();
System.out.println(month.getDisplayName(TextStyle.FULL, locale));
System.out.println(month.getDisplayName(TextStyle.NARROW, locale));
System.out.println(month.getDisplayName(TextStyle.SHORT, locale));
en本地输出
August
A
Aug
[[日期]]