Java 使用Duration类,获取两个日期(ZonedDateTime、LocalDateTime等)的差值(秒差、分钟差、小时差、天数差等)

使用Duration类,这是Java提供专门用来计算时间差的类。使用between静态方法传入要两个计算差值的时间对象。

代码:

// 比较LocalDateTime 
LocalDateTime dateTime1 = LocalDateTime.of(2022, 11, 5, 11, 50, 0);
LocalDateTime dateTime2 = LocalDateTime.of(2022, 11, 8, 15, 55, 1);
Duration between = Duration.between(dateTime1, dateTime2);
System.out.println(between.toDays()); // 3 相差3天
System.out.println(between.toHours()); // 76 相差76小时
System.out.println(between.toMinutes()); // 4565 相差4565分钟
System.out.println(between.getSeconds()); // 273901 相差273901秒
System.out.println(between.toMillis()); // 273901000 相差273901000毫秒
System.out.println(between.toNanos()); // 273901000000000 相差273901000000000纳秒

// 比较ZonedDateTime 
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime zonedDateTime = ZonedDateTime.now().plusHours(4);
Duration between1 = Duration.between(now, zonedDateTime);
System.out.println(between1.toHours()); // 4
System.out.println(between1.toMinutes()); // 240

注:

  • between传入两个时间,当第二个时间大于第一个时,差值为正数,反之为负数
  • 时间对象必须具有秒值,如ZonedDateTime、LocalDateTime、LocalTime都可以。使用LocalDate等无秒值的比较时,会出现 java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Seconds异常
  • 两个时间对象可以为不同对象。例如可以一个为ZonedDateTime,一个为LocalDateTime
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
问题:localdatetime 两个时间 解答过程: 首先,我们需要了解Java中的Date和LocalDateTime型。Date是一个表示日期和时间的,它通常用于处理旧版本的Java库中的日期和时间。LocalDateTimeJava 8引入的一个新,它表示一个不带时区的日期和时间。 在这个问题中,我们需要比较两个不同型的日期和时间:Date和LocalDateTime。为了进行比较,我们需要将它们转换为相同的型。我们可以通过以下步骤来实现这一点: 1. 将Date转换为LocalDateTime:我们可以使用Date对象的toInstant()方法将其转换为Instant对象,然后使用Instant对象的atZone()方法将其转换为ZonedDateTime对象。最后,我们可以使用ZonedDateTime对象的toLocalDateTime()方法将其转换为LocalDateTime对象。 2. 将LocalDateTime转换为Date:我们可以使用LocalDateTime对象的atZone()方法将其转换为ZonedDateTime对象,然后使用ZonedDateTime对象的toInstant()方法将其转换为Instant对象。最后,我们可以使用Instant对象的toLocalDateTime()方法将其转换回LocalDateTime对象。 现在我们可以比较这两个日期和时间了。以下是一个示例代码: ```java import java.time.*; public class DateAndLocalDateTimeComparison { public static void main(String[] args) { // 创建一个Date对象 Date date = new Date(); System.out.println("Date: " + date); // 将Date对象转换为LocalDateTime对象 LocalDateTime localDateTime = dateToLocalDateTime(date); System.out.println("LocalDateTime: " + localDateTime); // 创建一个新的Date对象 Date newDate = new Date(); System.out.println("New Date: " + newDate); // 将新的Date对象转换为LocalDateTime对象 LocalDateTime newLocalDateTime = newDateToLocalDateTime(newDate); System.out.println("New LocalDateTime: " + newLocalDateTime); } // 将Date对象转换为LocalDateTime对象的方法 public static LocalDateTime dateToLocalDateTime(Date date) { return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); } // 将新的Date对象转换为LocalDateTime对象的方法 public static LocalDateTime newDateToLocalDateTime(Date date) { return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值