本文最后更新于 354 天前,其中的信息可能已经有所发展或是发生改变。
工具类
时间日期工具类
java.time.LocalDate
| 方法 |
描述 |
LocalDate now() |
获取当前日期 |
LocalDate of(...) |
创建指定日期 |
LocalDate parse(CharSequnce text,[TemporalQuery<T> query] |
将字符串解析为日期或解析为指定格式化器的日期 |
LocalDate plusDays(long days) |
增加天数 |
LocalDate minusDays(long days) |
增加天数 |
boolean isBefore(LocalDate other) |
判断是否在另一个日期之前 |
boolean isAfter(LocalDate other) |
判断是否在另一个日期之后 |
int getYear() |
获取年 |
int getMonth() |
获取月 |
int getDayOfMonth() |
获取日 |
java.time.LocalTime
java.time.LocalDateTime
| 方法 |
描述 |
ZoneDateTime atZone(String zone) |
根据时区转换为ZoneDateTime对象 |
java.time.Instant
| 方法 |
描述 |
Instant now() |
获取系统当前时间对象 |
long getEpochSecond() |
获取秒级时间戳 |
Instant ofEpochMillis(long epocMilli) |
根据毫秒级时间戳创建时间戳 |
long toEpochMilli() |
获取毫秒级时间戳 |
int getNano() |
获取纳秒部分 |
java.time.Duration
| 方法 |
描述 |
Duration between(Temporal startInclusive, Temporal endExclusive) |
计算两个时间点之间的时间差 |
long toMinutes() |
返回时间差的分钟数 |
java.time.Period
| 方法 |
描述 |
Period between(LocalDate startDateInclusive, LocalDate endDateExclusive) |
计算两个日期之间的间隔 |
java.time.ZoneDateTime
| 方法 |
描述 |
ZoneDateTime now() |
获取当前带时区的日期时间 |
ZoneDateTime of(...,ZoneId zone) |
创建指定时区的日期时间 |
ZoneId getZone() |
返回当前时区 |
Instant toInstant() |
将ZoneDateTime对象转换为时间戳 |
java.time.format.DateTimeFormatter
| 方法 |
描述 |
DateTimeFormatter ofPattern(String pattern) |
创建自定义格式化器 |
String format(TemporalAccessor temporal) |
格式化日期时间 |
ZoneId getZone() |
返回当前时区 |
java.time.ZoneId
| 方法 |
描述 |
ZoneId systemDefault() |
获取系统默认时区,Asia/Shanghai 上海时区 |
ZoneId of(String zone) |
根据时区信息获取对应时区 |