Java 计算两个日期的差值

转载自:https://www.ripjava.com/article/1294911926173728

叙述

在这篇快速的文章中,我们将探讨Java中计算两个日期之间差值的一些方法。

解决方案

使用 java.util.Date

让我们首先使用Java SE API计算两个日期之间的天数:

@Test
public void test_TwoDatesDiffBeforeJava8()
  throws ParseException {

  SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", Locale.ENGLISH);
  Date firstDate = sdf.parse("2019/07/24");
  Date secondDate = sdf.parse("2019/07/30");

  long diffInMillis = Math.abs(secondDate.getTime() - firstDate.getTime());
  long diff = TimeUnit.DAYS.convert(diffInMillis, TimeUnit.MILLISECONDS);

  assertEquals(diff, 6);
}

使用Period和Duration

Java 8之后,我们可以使用LocalDate,LocalDateTime来表示两个日期(有或没有时间), 然后使用PeriodDuration来计算差值:

使用LocalDate:

@Test
public void  test_TwoDatesDiffInJava8() {
  LocalDate now = LocalDate.now();
  LocalDate sixDaysBehind = now.minusDays(6);

  Period period = Period.between(now, sixDaysBehind);
  int diff = Math.abs(period.getDays());

  assertEquals(diff, 6);
}

使用LocalDateTime:

@Test
public void test_TwoDateTimesInJava8() {
  LocalDateTime now = LocalDateTime.now();
  LocalDateTime sixMinutesBehind = now.minusMinutes(6);

  Duration duration = Duration.between(now, sixMinutesBehind);
  long diff = Math.abs(duration.toMinutes());

  assertEquals(diff, 6);
}

使用 java.time.temporal.ChronoUnit的between方法

Java 8中的时间日期 API使用TemporalUnit 接口表示时间的单位。比如天和秒。每个单元都提供了between方法的实现。我们可以也使用特定时间的单位计算两个日期之间的差值。

使用LocalDate:

@Test
public void test_TwoDatesDiffInJava8ByChronoUnit() {
  LocalDate now = LocalDate.now();
  LocalDate sixDaysAfter = now.plusDays(6);

  long diff = ChronoUnit.DAYS.between(now, sixDaysAfter);

  assertEquals(diff, 6);
}

使用LocalDateTime:

@Test
public void test_TwoDateTimesInJava8ByChronoUnit() {
  LocalDateTime now = LocalDateTime.now();
  LocalDateTime tenSecondsLater = now.plusSeconds(10);

  long diff = ChronoUnit.SECONDS.between(now, tenSecondsLater);

  assertEquals(diff, 10);
}

ChronoUnit 通过实现 TemporalUnit接口提供一组具体的时间单位。使用ChronoUnit时,建议使用静态导入,这样代码可读性会强一些。

import static java.time.temporal.ChronoUnit.SECONDS;
 
// 比如
long diff = SECONDS.between(now, tenSecondsLater);

我们可以将任意的时间对象Temporal传递给 between 方法。即使是表示不同的时区的ZonedDateTime

@Test
public void test_TwoZonedDateTimesDiffInJava8() {
  LocalDateTime ldt = LocalDateTime.now();
  ZonedDateTime now = ldt.atZone(ZoneId.of("Asia/Tokyo"));
  ZonedDateTime sixMinutesBehind = now
    .withZoneSameInstant(ZoneId.of("Asia/Shanghai"))
    .minusMinutes(6);

  long diff = ChronoUnit.MINUTES.between(sixMinutesBehind, now);

  assertEquals(diff, 6);
}

使用 java.time.temporal.Temporal的until()方法

任何Temporal对象,比如 LocalDate或ZonedDateTime,都提供了until方法来计算与指定的另一个时间的差值:

@Test
public void test_TwoZonedDateTimesDiffInJava8ByTemporal() {
  LocalDateTime now = LocalDateTime.now();
  LocalDateTime tenSecondsLater = now.plusSeconds(10);

  long diff = now.until(tenSecondsLater, ChronoUnit.SECONDS);

  assertEquals(diff, 10);
}

在本文中,我们演示了几种计算日期(有时间和无时间)之间差值的方法

最后,往常一样,代码可以在Github上找到。

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值