In Java 8 and later, use the java.time framework (Tutorial).
Duration
The Duration
class represents a span of time as a number of seconds plus a fractional second. It can count days, hours, minutes, and seconds.
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime oldDate = now.minusDays(1).minusMinutes(10);
Duration duration = Duration.between(oldDate, now);
System.out.println(duration.toDays());
ChronoUnit
If all you need is the number of days, alternatively you can use the ChronoUnit
enum. Notice the calculation methods return a long
rather than int
.
long days = ChronoUnit.DAYS.between( then, now );