如何计算两个日期之间的差距?

阅读量: 2594 编辑

如何计算两个日期之间的差距?

private static long daysBetween(Date one, Date two) {
    long difference =  (one.getTime()-two.getTime())/86400000;
    return Math.abs(difference);
}

使用joda-time包

<dependency>
  <groupId>joda-time</groupId>
  <artifactId>joda-time</artifactId>
  <version>2.10</version>
</dependency>
public static int daysBetweenUsingJoda(Date d1, Date d2){
    return Days.daysBetween(
           new LocalDate(d1.getTime()), 
           new LocalDate(d2.getTime())).getDays();
}

苏ICP备13052010号-3
©2022 南京匠成信息科技有限公司