如何计算日期的差异?

How to calculate the difference of dates?

我正在使用 java.util.Date class 并且想要比较 Date 对象和当前时间。

输出格式必须是这样的:

... years , ... months , ... days , ...hours , ... minutes , ... seconds.

这是我的代码:

public static void main(String[] args) {
    Calendar calendar = new GregorianCalendar(2022, 1 , 25 , 12 , 20 , 33);
    Date now = new Date(); //Tue Feb 25 11:49:05 IRST 2020
    Date date = calendar.getTime(); //Fri Feb 25 12:20:33 IRST 2022
}

有没有简单的计算方法?

Java 8 Date/Time API 比较日期灵活。

示例:

LocalDate currentDate = LocalDate.now();
LocalDate previousDate = LocalDate.of(2000, 7, 1);

Period period = Period.between(previousDate, currentDate);

String output = String.format("%s years , %s months , %s days",
                period.getYears(), period.getMonths(), period.getDays());

System.out.println(output);   
> 19 years , 7 months , 24 days

更多示例here

如果您想在 PeriodDuration 之间做出选择,

This 可能会对您有所帮助。

使用java即时

Instant start, end;
Duration duration = Duration.between(start, stop);
long hours = dur.toHours();
long minutes = dur.toMinutes();

java.time and why & how to use it

对于年、月、日的区别,有一个java.time.Period可以很方便的使用得到你想要的:

public static void main(String[] args) {
    LocalDateTime localDateTime = LocalDateTime.of(2022, 1 , 25 , 12 , 20 , 33);
    LocalDateTime now = LocalDateTime.now();

    // get the difference in years, months and days
    Period p = Period.between(now.toLocalDate(), localDateTime.toLocalDate());

    // and print the result(s)
    System.out.println("Difference between " + localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
            + " and " + now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + " is:\n"
            + p.getYears() + " years, " + p.getMonths() + " months, " + p.getDays() + " days");
}

此代码示例的输出将是(当然取决于当天):

Difference between 2022-01-25T12:20:33 and 2020-02-25T10:52:43.327 is:
1 years, 11 months, 0 days

您可以使用其他答案之一所示的 java.time.Duration 以获得小时、分钟和秒的额外差异。看这个例子(基本上就是上面那个加上时间计算):

public static void main(String[] args) {
    LocalDateTime localDateTime = LocalDateTime.of(2022, 1 , 25 , 12 , 20 , 33);
    LocalDateTime now = LocalDateTime.now();

    // get the difference in years, months and days (date related difference)
    Period p = Period.between(now.toLocalDate(), localDateTime.toLocalDate());
    // and the difference in hours, minutes and seconds (time-of-day related difference)
    Duration d = Duration.between(now.toLocalTime(), localDateTime.toLocalTime());
    long totalSeconds = d.getSeconds();
    long hours = totalSeconds / 3600;
    long minutes = (totalSeconds % 3600) / 60;
    long seconds = totalSeconds % 60;

    // and print the result(s)
    System.out.println("Difference between " + localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
            + " and " + now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + " is:\n"
            + p.getYears() + " years, " + p.getMonths() + " months, " + p.getDays() + " days, "
            + hours + " hours, " + minutes + " minutes, " + seconds + " seconds");
}

输出:

Difference between 2022-01-25T12:20:33 and 2020-02-25T11:25:42.712 is:
1 years, 11 months, 0 days, 0 hours, 54 minutes, 50 seconds

如果您收到 java.util.Dates 或扩展遗留代码(或者如果您懒得将所有代码更改为使用 java.time),您可以使用以下兼容性方法:

LocalDateTime fromDate = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
Date fromLocalDateTime = Date.from(LocalDateTime.now().toInstant((ZoneOffset.UTC)));