Instant,LocalDate,LocalTime,LocalDateTime和ZonedDateTime
文章目录
Instant
封装了从 1970-01-01T00:00:00Z 开始的秒数,相当于时间戳。
主要有两个属性:
private final long seconds;
private final int nanos;
LocalDate
用于表示日期,包括年、月、日,例如 2017-12-03。
主要有三个属性:
private final int year;
private final short month;
private final short day;
LocalTime
用于表示时间,包括时、分、秒,例如 10:15:30。
主要有四个属性:
private final byte hour;
private final byte minute;
private final byte second;
private final int nano; //纳秒
LocalDateTime
符合 ISO-8601 时间表示标准,用于表示日期和时间,包括年、月、日、时、分、秒,例如 2007-12-03T10:15:30。LocalDateTime 不包含时区信息。
LocalDateTime 只有两个属性:
private final LocalDate date; //日期
private final LocalTime time; //时间
ZonedDateTime
包含日期、时间、时区信息,例如 2007-12-03T10:15:30+01:00 Europe/Paris
主要有三个属性:
private final LocalDateTime dateTime; //时间
private final ZoneOffset offset; //相对于UTC的偏移量
private final ZoneId zone; //时区
关联
flowchart BT
纳秒-->Instant
秒-->Instant
纳秒-->LocalTime
秒-->LocalTime
分-->LocalTime
时-->LocalTime
天-->LocalDate
月-->LocalDate
年-->LocalDate
LocalTime-->LocalDateTime
LocalDate-->LocalDateTime
LocalDateTime-->ZonedDateTime
时区-->ZonedDateTime
时区偏移量-->ZonedDateTime
Instant,LocalDate,LocalTime,LocalDateTime 和 ZonedDateTime 共同的特征是,都是不可变和线程安全的。
共同方法
now()
获取当前时间。
LocalDateTime localDateTime = LocalDateTime.now(); //2023-10-29T15:18:15.186459200
LocalDate localDate = LocalDate.now(); //2023-10-29
of()
根据属性创建实例。
LocalDateTime localDateTime=LocalDateTime.of(2023,10,28,0,0); //2023-10-28T00:00
from()
从 temporal 创建实例。
parse()
解析字符串创建实例。
LocalDateTime parse = LocalDateTime.parse("2023-10-29T12:00:00"); //2023-10-29T12:00
get()
获取指定属性的数据,例如分钟。
with()
对日期时间进行调整,并返回调整后日期时间的副本。
主要存在两个方法,一个是 with(TemporalAdjuster adjuster)
,通过调整器进行调整。底层调用的是 with(TemporalField field, long newValue)
方法。
LocalDateTime with = localDateTime
.with(Month.NOVEMBER) //先调整为11月
.with(TemporalAdjusters.lastDayOfMonth()); //再调整为月底,结果为 2023-11-30T00:00
二是 with(TemporalField field, long newValue)
,通过将属性(年、月、日等)设置为新的值。
LocalDateTime localDateTime=LocalDateTime.of(2023,10,28,0,0);
LocalDateTime with = localDateTime.with(ChronoField.MONTH_OF_YEAR, Month.NOVEMBER.getValue()); //将月设置为11月,结果为2023-11-28T00:00
plus()
将日期时间加上指定值,并返回添加后的副本。
主要存在两种方法,一是 plus(TemporalAmount amountToAdd)
,参数通常为实现 TemporalAmount
接口的 Duration
和 Period
,分别代表一段时间和一段日期。底层调用的是 plus(long amountToAdd, TemporalUnit unit)
方法。
LocalDateTime localDateTime=LocalDateTime.of(2023,10,28,0,0);
LocalDateTime plus = localDateTime.plus(Period.ofYears(1)); //将日期加上1年,结果为2024-10-28T00:00
二是 plus(long amountToAdd, TemporalUnit unit)
,通过将属性直接加上指定值。
LocalDateTime localDateTime=LocalDateTime.of(2023,10,28,0,0);
LocalDateTime plus = localDateTime.plus(1, ChronoUnit.YEARS); //将日期加上1年,结果为2024-10-28T00:00
minus()
将日期时间减去指定值,底层调用的是 plus(long amountToAdd, TemporalUnit unit)
。只是将 amountToAdd 改成了负数。
until()
long until(Temporal endExclusive, TemporalUnit unit)
,endExclusive 为结束时间。
计算两个日期时间之间在某个属性方面的差值。
LocalDateTime localDateTime=LocalDateTime.of(2023,10,28,0,0);
LocalDateTime localDateTime2=LocalDateTime.of(2023,11,28,0,0);
long until = localDateTime.until(localDateTime2, ChronoUnit.MONTHS); //两个时间相差了1个月
long days = localDateTime.until(localDateTime2, ChronoUnit.DAYS); //也等同于相差了31天
format()
对代码进行格式化。
LocalDateTime localDateTime=LocalDateTime.of(2023,10,28,0,0);
DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd");
String format = localDateTime.format(dateTimeFormatter); //结果为2023-10-28
isAfter()
判断当前时间是否晚于指定时间。
LocalDateTime localDateTime=LocalDateTime.of(2023,10,28,0,0);
LocalDateTime localDateTime2=LocalDateTime.of(2023,11,28,0,0);
boolean after = localDateTime.isAfter(localDateTime2); //false
boolean after1 = localDateTime2.isAfter(localDateTime); //true
isBefore()
判断当前时间是否早于指定时间。
isEqual()
判断当前时间是否与指定时间相等。
相互转换
flowchart LR
ZonedDateTime -- "toLocalDateTime()" -->LocalDateTime
ZonedDateTime -- "toLocalDate()" -->LocalDate
ZonedDateTime -- "toLocalTime()" -->LocalTime
ZonedDateTime -- "toEpochSecond()" -->时间戳
ZonedDateTime -- "toInstant()" -->Instant
LocalDateTime -- "toLocalDate()" -->LocalDate
LocalDateTime -- "toLocalTime()" -->LocalTime
LocalDateTime -- "atZone(ZoneId)" -->ZonedDateTime
LocalDateTime -- "toEpochSecond(ZoneOffset)" -->时间戳
LocalDateTime -- "toInstant(ZoneOffset)" -->Instant
LocalDate--"atTime()" -->LocalDateTime
LocalDate--"toEpochSecond(LocalTime, ZoneOffset)" -->时间戳
LocalTime--"atDate(LocalDate)"-->LocalDateTime
LocalTime--"toEpochSecond(LocalDate, ZoneOffset)"-->时间戳
Instant--"atZone(ZoneId)"-->ZonedDateTime
Instant--"toEpochMilli()/1000"-->时间戳
能看到转换方法主要以 to
,at
为前缀,并且 LocalDate
,LocalTime
,LocalDateTime
需要先转换为 ZonedDateTime
再转换为时间戳。
文章作者 梧桐碎梦
上次更新 2023-10-29 20:24:47