使用 Java 8 的新特性(DateTime)来管理日期和时间

前言
在 Java 中,日期和时间处理的核心类位于 java.time 包下,Java 8 引入了新的日期和时间 API,主要包括以下几个关键类:

LocalDate:用于表示日期,不包含时间信息,例如:2023-09-03。
LocalTime:用于表示时间,不包含日期信息,例如:15:30:45。
LocalDateTime:用于表示日期和时间,不包含时区信息,例如:2023-09-03T15:30:45。
ZonedDateTime:用于表示带时区的日期和时间,例如:2023-09-03T15:30:45+01:00。
Instant:用于表示从纪元时间(1970-01-01T00:00:00Z)起的时间戳,精确到秒和纳秒。
Duration:用于表示时间段的持续时间,以秒和纳秒为单位。
Period:用于表示日期之间的间隔,以年、月和日为单位。
DateTimeFormatter:用于格式化和解析日期和时间的类。

应用

DateTime取代Date类,该类的API修复了不合理的常量表示,严格按照ISO 8601规定的日期和时间格式进行输出,ISO 8601通过T进行日期和时间的分隔。
比如:

  • Month的范围用1~12表示1月到12月;
  • Week的范围用1~7表示周一到周日。

1、获取当前时间

LocalDate d = LocalDate.now(); // 当前日期
LocalTime t = LocalTime.now(); // 当前时间
LocalDateTime dt = LocalDateTime.now(); // 当前日期和时间

2、指定的日期和时间创建

// 指定各独立的日期和时间:
LocalDate d2 = LocalDate.of(2022, 4, 20); // 2022-04-20, 注意04=4月
LocalTime t2 = LocalTime.of(15, 16, 17); // 15:16:17
LocalDateTime dt2 = LocalDateTime.of(2022, 4, 20, 15, 16, 17);//2022-04-20T15:16:17
LocalDateTime dt3 = LocalDateTime.of(d2, t2);//2022-04-20T15:16:17

//传入时间字符串
LocalDate d = LocalDate.parse("2022-04-20");
LocalTime t = LocalTime.parse("15:16:17");
LocalDateTime dt3 = LocalDateTime.parse(" 2022-04-20 15:16:17");

3、加减日期

LocalDateTime dt = LocalDateTime.now();
// 加1天减1小时:
LocalDateTime dt2 = dt.plusDays(1).minusHours(1);
// 减1月:
LocalDateTime dt3 = dt2.minusMonths(1);
System.out.println(dt3);
//对时间进行操作,出现跨年、跨月操作时,时间会自动进行转换,比如:2019-10-31减去1个月得到的结果是2019-09-30,因为9月没有31日。

4、时间调整

对日期和时间进行调整则使用withXxx()方法,例如:withHour(15)会把10:11:12变为15:11:12:

调整年:withYear()
调整月:withMonth()
调整日:withDayOfMonth()
调整时:withHour()
调整分:withMinute()
调整秒:withSecond()

LocalDate dt = LocalDate.of(2020, 3, 31); //2020-03-31
System.out.println(dt);
// 月份变为4:
LocalDate dt3 = dt.withMonth(4);
System.out.println(dt3); //2020-04-30
//对时间进行操作,出现跨年、跨月操作时,时间会自动进行转换,比如:2020-03-31调整为4个月得到的结果是2020-04-30,因为4月没有31日。

5、时间调整进阶配合 TemporalAdjusters

 dayOfWeekInMonth //它的值为同一个月中每一周的第几天
 firstDayOfMonth //它的值为当月的第一天
 firstDayOfNextMonth //它的值为下月的第一天
 firstDayOfNextYear //它的值为明年的第一天
 firstDayOfYear //它的值为当年的第一天
 firstInMonth //它的值为同一个月中,第一个符合星期几要求的值
 lastDayOfMonth //它的值为当月的最后一天
 lastDayOfYear //它的值为今年的最后一天
 //对指定星期的操作 - DayOfWeek
 lastInMonth //它的值为同一个月中,符合星期几要求的值
 next/previous//将日期向前一周或者向后一周,调整到指定星期几
 nextOrSame/previousOrSame //将日期向前或者向后,调整到指定星期几(如当前日期不存在于调整周的区间内则跨周),比如调整到周四,如果当天周二,则为本周周四;如果当天周五,则为下周四
     
//实列
LocalDate dt = LocalDate.now();
//明年的第一天
System.out.println(dt.with(TemporalAdjusters.firstDayOfNextYear()));
//两周后的周五
System.out.println(dt.with(TemporalAdjusters.next(DayOfWeek.FRIDAY)).plusWeeks(1));

6、时区操作

ZonedDateTime zbj = ZonedDateTime.now(); // 默认时区时间
ZonedDateTime zny = ZonedDateTime.now(ZoneId.of("America/New_York"));//指定时区获取当前时间

//atZone时区转换,通过ZoneId指定要转换到的时区
//LocalDateTime转ZonedDateTime
LocalDateTime ldt = LocalDateTime.now();
//2020-04-15T15:16:17+08:00[Asia/Shanghai] 获取吗默认时区
ZonedDateTime zbj = ldt.atZone(ZoneId.systemDefault());
//2020-04-15T03:16:17-04:00[America/New_York],与美国差12小时
ZonedDateTime zny = ldt.atZone(ZoneId.of("America/New_York"));

//时区转换
//转为指定时区时间
ZonedDateTime zny = ldt.withZoneSameInstant(ZoneId.of("America/New_York"));
//转为当前时区时间
LocalDateTime date = zny.toLocalDateTime();

7、时间戳操作

System.currentTimeMillis();//毫秒级时间戳
Instant now = Instant.now();
System.out.println(now.getEpochSecond()); // 秒
System.out.println(now.toEpochMilli()); // 毫秒
Instant.ofEpochSecond(value);//设置秒时间戳
Instant.ofEpochMilli(value);//设置毫秒时间戳


//当前时间戳转换为指定时区
Instant ins = Instant.now();
ZonedDateTime zdt = ins.atZone(ZoneId.of("America/New_York"))
    
//时间戳转LocalDateTime
LocalDateTime time = LocalDateTime.ofInstant(Instant.ofEpochMilli(systemTime), ZoneId.systemDefault());

8、日期格式化

LocalDateTime dt = LocalDateTime .now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(dateTimeFormatter.format(dt));

9、时间差

LocalDate startDate=startDate;
LocalDate endDate=endDate;
long y = ChronoUnit.YEARS.between(startDate, endDate); //获取两个日期间隔年
long m = ChronoUnit.MONTHS.between(startDate, endDate);//获取两个日期间隔月
long d = ChronoUnit.DAYS.between(startDate, endDate); //获取两个日期间隔天

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LocalDateTime和Date是Java中处理日期时间的类。它们之间可以相互转换。 引用中的示例代码展示了如何将ZoneDateTime转换为Date。首先,我们需要一个Date的格式化对象。然后,我们可以通过以下步骤将ZoneDateTime转换为Date: 1. 将ZoneDateTime转换为Instant对象:使用`toInstant()`方法将ZoneDateTime转换为Instant。 2. 将Instant对象转换为Date对象:使用`Date.from(instant)`方法将Instant对象转换为Date。 引用中的示例代码展示了如何将Date转换为ZoneDateTime。同样,我们需要一个Date的格式化对象。然后,可以按以下步骤将Date转换为ZoneDateTime: 1. 将Date转换为Instant对象:使用`toInstant()`方法将Date转换为Instant。 2. 将Instant对象转换为ZoneDateTime对象:使用`ZonedDateTime.ofInstant(instant, zoneId)`方法将Instant对象转换为ZoneDateTime。 需要注意的是,ZoneDateTime是带有时区信息的日期时间类,而LocalDateTime没有时区信息。因此,在转换时需要注意时区的处理。 总结起来,可以通过上述步骤将ZoneDateTime和Date相互转换。这些转换可以帮助我们在Java中更方便地处理日期时间。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Java8特性 - LocalDate、LocalDateTime、ZoneDateTime 与 Date 的相互转换](https://blog.csdn.net/qq_39505245/article/details/123768265)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值