[!hint] 日期时间使用
java.time,而不是java.utils.Date后者有很多缺陷【非线程安全,设计不合理,不支持时区】
Instant
Instant 表示时间线上的一个点
Duration
Duration 表示持续的一个时间段
java
LocalDateTime start = LocalDateTime.of(2024, 1, 24, 10, 30, 0);
LocalDateTime end = LocalDateTime.of(2024, 1, 24, 12, 45, 0);
// 计算时间差
Duration duration = Duration.between(start, end);
// 获取秒数差值
long seconds = duration.getSeconds();❤️ LocalDate
修改 :
LocalDateTime atTime(LocalTime)给 LocalDate 转 LocalDateTime
❤️ LocalDateTime
[!quote] 日期时间对象有三种:
LocalDateTime日期时间LocalDate日期LocalTime时间
创建 :
- 【获取当前时间】
LocalDateTime.now()获取当前日期时间对象LocalDate.now()获取日期对象LocalTime.now()获取时间对象
- 【自定义时间】
LocalDateTime LocalDateTime.of(年, 月, 日, 时, 分, 秒)LocalDate LocalDate.of(年, 月, 日)LocalTime LocalTime.of(时, 分, 秒)
转换 :
- 【日期时间对象之间互转】
LocalDate toLocalDate()从日期时间对象获取日期对象LocalTime toLocalTime()从日期时间对象获取时间对象
- 【时间戳 -> LocalDateTime】
LocalDateTime LocalDateTime.ofInstant(Instant对象,时区)
java
// 将时间戳转换为Instant对象
Instant instant = Instant.ofEpochMilli(1725875842704L);
// 转换为中国上海时区的LocalDateTime
LocalDateTime localDateTime = LocalDateTime.ofInstant(
instant,
ZoneId.of("Asia/Shanghai"))
).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") ;- 【LocalDateTime -> 时间戳】
localDateTime.atZone(ZoneId.of("Asia/Shanghai")).toEpochSecond()设置时区,之后转时间戳
获取 :
int getYear()获取年Month getMonth()获取月【英文】int getMonthValue()获取月【数字】int getDayOfMouth()获取日int getHour()获取时int getMinute()获取分int getSecond()获取秒
java
// 获取
// 获取当前的日期时间对象
LocalDateTime currentTime = LocalDateTime.now();
System.out.println("当前时间: " + currentTime);
// 获取日期对象
LocalDate date1 = currentTime.toLocalDate();
System.out.println("date1: " + date1);
// 获取月
Month month = currentTime.getMonth();
int day = currentTime.getDayOfMonth();
int seconds = currentTime.getSecond();
System.out.println("月: " + month +", 日: " + day +", 秒: " + seconds);
---
当前时间: 2016-04-15T16:55:48.668
date1: 2016-04-15
月: APRIL, 日: 15, 秒: 48修改 :
- 【修改为指定的值】
with(TemporalAdjuster)例如 .with(DayOfWeek.MONDAY) 将日期修改为这一周的周一,传入的是时间,就只修改时间;传入的是日期,就只修改日期LocalDateTime withYear(int i)修改年LocalDateTime withMonth(int i)修改月LocalDateTime withDayOfMonth(int i)修改日LocalDateTime withHour(int hour)修改时LocalDateTime withMinute(int minute)修改分LocalDateTime withSecond(int second)修改秒LocalDateTime withNano(int nanoOfSecond)修改纳秒
- 【加减指定值】
plusHours(Long值)- ……
minusDays(Long值)- ……
格式化,解析 :
format(日期时间的格式)
java
String format = localDateTime.format(
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
);LocalDateTime.parse(时间字符串,时间格式)
java
LocalDateTime dateTime = LocalDateTime.parse("2024-04-19T00:11:58.906799300");
LocalDateTime dateTime = LocalDateTime.parse(
"10-10-2023 14:30",
DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm")
);❤ ZonedDateTime
获取时区日期时间对象
获取当前时间的时区日期时间对象
ZonedDateTime ZonedDateTime.now()
java
// 获取当前时间的时区日期时间对象
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("当前时间: " + zonedDateTime);
---
当前时间: 2024-04-19T13:13:15.270655200+08:00[Asia/Shanghai]从对象中获取对象
ZoneId getZone()返回一个时区对象ZoneOffset getOffset()返回一个时间差对象【表示当前时区与 UTC(世界标准时间) 的偏移量】- 其他日期,时间对象与
LocalDateTime类似 ……
java
// 从对象中获取对象
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime.getZone());
System.out.println(zonedDateTime.getOffset());
---
Asia/Shanghai
+08:00从字符串中获取对象
ZonedDateTime ZonedDateTime.parse(字符串)
java
// 从字符串中获取对象
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2021-01-01T10:15:30+05:30[Asia/Kolkata]");
System.out.println("时区日期时间: " + zonedDateTime);
---
时区日期时间: 2021-01-01T10:15:30+05:30[Asia/Kolkata]自定义对象
ZonedDateTime ZonedDateTime.of(参数 ……)自定义 ZonedDateTimeZoneId ZoneId.of(参数 ……)自定义 ZoneIdZoneOffset ZoneOffset.of(参数 ……)自定义 ZoneOffset
java
ZonedDateTime zonedDateTime = ZonedDateTime.of(2021, 1, 1, 10, 20, 30, 0, ZoneId.of("Asia/Shanghai"));
System.out.println("当前时间: " + zonedDateTime);
System.out.println("----------------------");
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
System.out.println("时区: " + zoneId);
System.out.println("----------------------");
ZoneOffset zoneOffset = ZoneOffset.of("+08:00");
System.out.println("时差: " + zoneOffset);
---
当前时间: 2021-01-01T10:20:30+08:00[Asia/Shanghai]
----------------------
时区: Asia/Shanghai
----------------------
时差: +08:00获取值
与 LocalDateTime 大同小异