How to calculate the difference of dates?

bizzaro33 :

Im using java.util.Date class and want to compare a Date object and the current time.

output format must be like this :

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

this is my code :

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
}

is there a simple way to calculate that?

deHaar :

java.time and why & how to use it

For the difference in years, months and days, there is a java.time.Period you can easily use to get what you want:

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");
}

The output of this code example would be (depending on the current day, of course):

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

You may use a java.time.Duration as shown in one of the other answers in order to get the additional difference in hours, minutes and seconds. See this example (which is basically the one from above plus time calculation):

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");
}

Output:

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

If you are receiving java.util.Dates or extending legacy code (or if you are just too lazy to change all your code to using java.time), you can make use of the following compatibility methods:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=12131&siteId=1