Java日期和时间处理全解析
1. 创建ZonedDateTime
在Java中,创建 ZonedDateTime 有多种方式,但考试中只需掌握以下三种:
public static ZonedDateTime of(int year, int month,
int dayOfMonth, int hour, int minute, int second,
int nanos, ZoneId zone)
public static ZonedDateTime of(LocalDate date, LocalTime time,
ZoneId zone)
public static ZonedDateTime of(LocalDateTime dateTime, ZoneId zone)
需要注意的是,这里不能传入 Month 枚举。同时,日期和时间类使用私有构造函数和静态方法返回实例,这就是工厂模式。例如,下面的代码无法编译:
var d = new LocalDate(); // DOES NOT COMPILE
另外,如果向 of() 方法传入无效数字,会抛出异常,如:
var d = LocalDate.of(2022, Month.JANUARY, 32) // DateTimeException
<
超级会员免费看
订阅专栏 解锁全文
1221

被折叠的 条评论
为什么被折叠?



