admin管理员组文章数量:1595923
文章目录
- 学习日记(常用类之 JDK8 新增的时间日期类:LocalDate、LocalTime、LocalDateTime、Instant、DateTimeFormatter、Period、Duration、ChronoUnit 类)
- 一、LocalDate、LocalTime、LocalDateTime 类
- 1. 创建对象
- 2. 调用 API
- 3. 不同类相互转换
- 4. 修改相关的 API
- 二、Instant 类
- 三、DateTimeFormatter 类
- 四、Period 类和 Duration 类
- 1. Period 类
- 2. Duration 类
- 五、ChronoUnit 类
- 注意:
学习日记(常用类之 JDK8 新增的时间日期类:LocalDate、LocalTime、LocalDateTime、Instant、DateTimeFormatter、Period、Duration、ChronoUnit 类)
从 java 8 开始,java.time 包提供了新的日期和时间 API,主要有以下:
类名 | 说明 |
---|---|
LocalDate | 只含日期 |
LocalTime | 只含时间 |
LocalDateTime | 含日期和时间 |
Instant | 代表时间戳 |
DateTimeFormatter | 用于时间的格式化和解析 |
Duration | 用于计算两个时间的间隔 |
Period | 用于计算两个日期的间隔 |
ChronoUnit | 可用于比较所有的时间单位 |
- 新增的 API 区分了时刻、本地日期、本地时间,并且,对日期和时间的进行运算更加方便。
- 新增的 API 的类型几乎是不变类型(和 String 类型的使用类似),可以放心使用不必担心被修改。
一、LocalDate、LocalTime、LocalDateTime 类
- 他们的类的实例是不可变的对象。
- 他们三者构建的对象和 API 都是通用的。
1. 创建对象
三者通过调用方法来创建对象,如
- 现在的日期时间,用
now
方法:LocalDateTime nowDateTime = LocalDateTime.now();
- 指定的日期时间,用
of
方法:LocalDate t1 = LocalDate.of(2022, 9, 3);
2. 调用 API
类中有非常多的 API,可以通过查找 API 文档来调用。
3. 不同类相互转换
方法名 | 说明 |
---|---|
public LocalDate toLocalDate() | LocalDateTime 对象 -> LocalDate 对象 |
public LocalTime toLocalTime() | LocalDateTime 对象 -> LocalTime 对象 |
public static LocalDateTime of(LocalDate date, LocalTime time) | LocalDate、LocalTime 对象 -> LocalDateTime 对象 |
4. 修改相关的 API
方法名 | 说明 |
---|---|
plusDays、plusWeeks、plusMonths、plusYears… | 向当前对象增加几天、几周、几个月、几年… |
minusDays、minusWeeks、minusMonths、minusYears | 向当前对象减少几天、几周、几个月、几年… |
withDayofMonth、withDayofYear、withMonth、withYear | 将当前对象修改到指定的值,并返回新的 LocalDate 对象 |
isBefore(在前)、isAfter(在后)、equals(同一天) | 比较两个 LocalDate 对象,返回值为 boolean 类型 |
注意:这些方法返回的都是一个新的实例引用,因为LocalDate、LocalTime、LocalDateTime 都是不可变的!
二、Instant 类
- JDK 8 获取时间戳特别简单,功能丰富,获取时间戳的方法为调用
now
方法:Instant now = Instant.now();
。 - 时间戳包含日期和时间,与
java.util.Date
类似,相当于Date
类。 - Instant 类和 Date 类可以相互转换。
方法 | 说明 |
---|---|
public static Instant now() | 获取当前时间戳,static 方法,Instant 类直接调用 |
public static Date from(Instant instant) | Instant -> Date,Date 类中的方法,Date 类直接调用 |
public Instant toInstant() | Date -> Instant,Date 类中的方法,创建 Date 类对象调用 |
三、DateTimeFormatter 类
DateTimeFormatter 类是 JDK 8 新引入的日期与时间格式器,正反都能调用 format 方法。
方法 | 说明 |
---|---|
public String format() | 格式化,LocalDateTime 类 -> String 类,正向或逆向格式化都可以 |
public static LocalDateTime parse(String str, DateTimeFormatter d) | 解析,String 类 -> LocalDateTime 类,是 LocalDateTime 的静态方法,类直接调用,返回类型为 LocalDateTime 类 |
注意:LocalDate 类 -> String 类(格式化)或 String 类 -> LocalDate 类(解析)也可以!
四、Period 类和 Duration 类
1. Period 类
- 在 java 8 中,可以使用
java.time.Period
来计算日期间隔。 - Period 类用于 LocalDate 对象之间的比较。
- 通过 between 方法创建 Period 类的对象:
Period period = Period.between(birthday, today);
。
方法名 | 说明 |
---|---|
public static Period between(LocalDate startDate, LocalDate endDate) | 通过 between 方法创建 Period 类的对象,static 方法 |
public int getYears() | 年 |
public int getMonths() | 月 |
public int getDays() | 日 |
2. Duration 类
- 在 java 8 中,可以使用
java.time.Duration
来计算时间间隔。 - Duration 类用于 LocalDateTime 对象或 Instant 对象之间的比较。
- 通过 between 方法创建 Duration 类的对象:
Duration duration = Duration.between(birthday, today);
。
方法名 | 说明 |
---|---|
public static Duration between(LocalDateTime start, LocalDateTime end) | 通过 between 方法创建 Duration 类的对象,static 方法 |
public long toDays() | 两个时间差的天数,精确到天 |
public long toHours() | 两个时间差的小时数,精确到小时 |
public long toNanos() | 两个时间差的纳秒数,精确到纳秒 |
五、ChronoUnit 类
ChronoUnit 类可用于在单个时间单位内测量一段时间,是最全的工具类,可用于比较所有的时间单位。
注意:
- 关于 Period 类中的方法
- 所有的时间类和方法在需要用到时去查就可以。
本文标签: 常用日期时间日记LocalDateTime
版权声明:本文标题:学习日记(常用类之 JDK8 新增的时间日期类:LocalDateTime、Instant、DateTimeFormatter、Period、Duration、ChronoUnit 类) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1728237370a1150569.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论