I am trying to calculate the difference between two LocalDateTime
.
The output needs to be of the format y years m months d days h hours m minutes s seconds
. Here is what I have written:
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
public class Main {
static final int MINUTES_PER_HOUR = 60;
static final int SECONDS_PER_MINUTE = 60;
static final int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR;
public static void main(String[] args) {
LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 9, 19, 46, 45);
LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55);
Period period = getPeriod(fromDateTime, toDateTime);
long time[] = getTime(fromDateTime, toDateTime);
System.out.println(period.getYears() + " years " +
period.getMonths() + " months " +
period.getDays() + " days " +
time[0] + " hours " +
time[1] + " minutes " +
time[2] + " seconds.");
}
private static Period getPeriod(LocalDateTime dob, LocalDateTime now) {
return Period.between(dob.toLocalDate(), now.toLocalDate());
}
private static long[] getTime(LocalDateTime dob, LocalDateTime now) {
LocalDateTime today = LocalDateTime.of(now.getYear(),
now.getMonthValue(), now.getDayOfMonth(), dob.getHour(), dob.getMinute(), dob.getSecond());
Duration duration = Duration.between(today, now);
long seconds = duration.getSeconds();
long hours = seconds / SECONDS_PER_HOUR;
long minutes = ((seconds % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE);
long secs = (seconds % SECONDS_PER_MINUTE);
return new long[]{hours, minutes, secs};
}
}
The output that I am getting is 29 years 8 months 24 days 12 hours 0 minutes 50 seconds
. I have checked my result from this website (with values 12/16/1984 07:45:55
and 09/09/2014 19:46:45
). The following screenshot shows the output:
I am pretty sure that the fields after the month value is coming wrong from my code. Any suggestion would be very helpful.
Update
I have tested my result from another website and the result I got is different. Here it is: Calculate duration between two dates (result: 29 years, 8 months, 24 days, 12 hours, 0 minutes and 50 seconds).
Update
Since I got two different results from two different sites, I am wondering if the algorithm of my calculation is legitimate or not. If I use following two LocalDateTime
objects:
LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 10, 6, 40, 45);
LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55);
Then the output is coming: 29 years 8 months 25 days -1 hours -5 minutes -10 seconds.
From this link it should be 29 years 8 months 24 days 22 hours, 54 minutes and 50 seconds
. So the algorithm needs to handle the negative numbers too.
Note the question is not about which site gave me what result, I need to know the right algorithm and need to have right results.
Period.between()
apply some rounding? – Thomas7:45:55
and the end time19:46:45
(or7:46:45
PM). So the difference between those two times is 12 hours, 0 minutes and 50 seconds and never 23 hours, 34 minutes and 12 seconds. So your calculation actualle seems to be correct, at least on the time part. – ThomasLocalDateTime
has no time zone, there might not be a unique answer. Even if you assume the start and end time zones are the same, in certain zones dates like 2014-09-09 will be in Daylight Saving Time or Summer Time and in others it will not. This might throw things off by an hour. So computing the difference to the second is meaningless unless this is resolved. – Stuart MarksLocalDateTime
yields unrealistic results, as that class purposely lacks any concept of time zone or offset-from-UTC? For realistic values, assign a time zone viaZoneId
to useZonedDateTime
. – Basil Bourque