# javaTime
# 格式化
枚举类FormatStyle定义了格式。
DateTimeFormatter预定义了基于本地(Locale)风格的时间格式。
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
.format(ZonedDateTime.now());
# DateTimeFormatter
| 方法 | 说明 |
|---|---|
| ofLocalizedDateTime | 本地化日期时间 |
| ofLocalizedTime | 本地化时间 |
| ofLocalizedDate | 本地化日期 |
# FormatStyle
| 方法 | 说明 | 说明 |
|---|---|---|
| FULL | 完整格式 | 2022年1月6日星期四 中国标准时间 下午3:58:51 |
| LONG | 长时间格式 | 2022年1月6日 CST 下午3:58:51 |
| MEDIUM | 中等时间格式 | 2022年1月6日 下午3:58:51 |
| SHORT | 短时间格式 | 2022/1/6 下午3:58 |
# ISO/RFC规范格式
DateTimeFormatter isoWeekDateFormatter = DateTimeFormatter.ISO_WEEK_DATE;
String format = isoWeekDateFormatter.format(LocalDateTime.now());
System.out.println("format = " + format);
//format = 2022-W01-4
DateTimeFormatter内置多种ISO格式
# 范式格式化
通过字母和符号来构建一个范式(Patterns),使用ofPattern(String)或者ofPattern(String, Locale)方法传递构建的范式。
String pattern = "G uuuu'年'MMMd'日' ZZZZZ VV";
String format= DateTimeFormatter.ofPattern(pattern).format(ZonedDateTime.now());
System.out.println("format = " + format);
| 方法 | 说明 |
|---|---|
| ` | 分割符 |
| '' | 不被解析的文字 |
| [ | 可选部分开始 |
| ] | 可选部分结束 |
| # | 保留字 |
| { | 保留字 |
| } | 保留字 |