Skip to content

[!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(参数 ……) 自定义 ZonedDateTime
  • ZoneId ZoneId.of(参数 ……) 自定义 ZoneId
  • ZoneOffset 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 大同小异