1.Instant

Java提供了Instant用来标识时间戳,可以很方便的获取到当前的时间戳,也就是从1970-01-01 00:00:00开始到当前时间所经过的秒数

  • Instant.now() 静态方法获取包含了当前时间戳的Instant实例对象
// 获取当前的时间的时间戳对象
Instant now = Instant.now();
System.out.println(now);
运行结果

java long时间相减 java时间戳相减_计算逻辑

  • instant.getEpochSecond() 实例方法,获取时间戳数值
// 获取当前的时间的时间戳对象
Instant now = Instant.now();
// 获取now对象表示的时间戳数值,也就是从1970-01-01 00:00:00开始到当前时间所经过的秒数
long epochSecond = now.getEpochSecond();
System.out.println(now);
System.out.println("当前的时间戳: " + epochSecond);
运行结果

java long时间相减 java时间戳相减_时间戳_02

2.Duration

JDK8提供了用来计算两个时间差的工具类DurationDuration.between(time1, time2)获取Duration实例对象

  • duration.toDays() 获取相差多少天
  • duration.toHours() 获取相差多少小时
  • duration.toMinutes() 获取相差多少分钟
  • duration.toMillis() 获取相差多少毫秒

时间差的计算逻辑为time2 - time1,如果time1表示的时间在time2之前也就是time1 < time2;以上方法返回的值是正数,反之;则返回的值为负数

// 获取当前时间
LocalTime now = LocalTime.now();
// 指定时间 12:30
LocalTime time = LocalTime.of(12, 30, 0);

System.out.println("当前时间为:" + now);
// 计算时间差
Duration duration = Duration.between(time, now);
System.out.println("相差 " + duration.toDays() + " 天");
System.out.println("相差 " + duration.toHours() + " 小时");
System.out.println("相差 " + duration.toMinutes() + " 分钟");
System.out.println("相差 " + duration.toMillis() + " 毫秒");
执行结果

java long时间相减 java时间戳相减_java_03


在上面的例子中,time < now 所以duration.toXXX们返回的事正数

// 指定时间 12:30
LocalTime time1 = LocalTime.of(12, 30, 0);
// 指定时间 9:30
LocalTime time2 = LocalTime.of(9, 30, 0);
System.out.println("time1: " + time1);
System.out.println("time2: " + time2);

// 计算时间差
Duration duration = Duration.between(time1, time2);
System.out.println("相差 " + duration.toDays() + " 天");
System.out.println("相差 " + duration.toHours() + " 小时");
System.out.println("相差 " + duration.toMinutes() + " 分钟");
System.out.println("相差 " + duration.toMillis() + " 毫秒");
执行结果

java long时间相减 java时间戳相减_java long时间相减_04


在上面的例子中,time1 > time2 所以duration.toXXX们返回的事正数

2.Period

Duration外JDK8还提供了Period用于计算日期差,Period的使用和Duration基本相似,Period.between(time1, time2)获取Period实例对象

  • period.toDays() 获取相差多少天
  • period.toHours() 获取相差多少小时
  • period.toMinutes() 获取相差多少分钟
  • period.toMillis() 获取相差多少毫秒

日期差的计算逻辑为date2 - date1,如果date1表示的时间在date2之前也就是date1 < date2;以上方法返回的值是正数,反之;则返回的值为负数

// 获取当前日期
LocalDate now = LocalDate.now();
// 获取指定日期 2008-08-08
LocalDate date = LocalDate.of(2008, 8, 8);
// 计算日期差
Period period = Period.between(date, now);
System.out.println("当前日期是:" + now);
System.out.println("2008-08-08 距今已经 " + period.getYears() + "年, "+ period.getMonths() +"个月, "+period.getDays()+"天");
执行结果

java long时间相减 java时间戳相减_计算逻辑_05


在上面的例子中,date < now 所以duration.getXXX们返回的是正数

// 获取指定日期 2060-01-01
LocalDate date1 = LocalDate.of(2060, 1, 1);
// 获取指定日期 2008-08-08
LocalDate date2 = LocalDate.of(2008, 8, 8);
System.out.println("date1: " + date1);
System.out.println("date2: " + date2);
// 计算日期差
Period period = Period.between(date1, date2);
System.out.println("2060-01-01 至今还有 " + period.getYears() + "年, "+ period.getMonths() +"个月, "+period.getDays()+"天");
执行结果

java long时间相减 java时间戳相减_System_06

在上面的例子中,date1 > date2 所以duration.getXXX们返回的是负数