Close

Java Date Time - LocalDateTime.until() Examples

Java Date Time Java Java API 


Class:

java.time.LocalDateTime

java.lang.Objectjava.lang.Objectjava.time.LocalDateTimejava.time.LocalDateTimejava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.time.chrono.ChronoLocalDateTimeChronoLocalDateTimejava.io.SerializableSerializableLogicBig

Method:

public long until(Temporal endExclusive,

TemporalUnit unit)

This method calculates the amount in provided unit between this date-time and the provided temporal.

In other words, this method returns the amount of time between two LocalDateTime objects in terms of a single TemporalUnit



Examples


package com.logicbig.example.localdatetime;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class UntilExample {

public static void main (String... args) {
LocalDateTime dt = LocalDateTime.of(1999, 1, 20,
20, 10, 30);
LocalDateTime dt2 = LocalDateTime.of(2005, 10, 1,
10, 5);

long l = dt.until(dt2, ChronoUnit.DAYS);
System.out.printf("DAYS between %s and %s => %s%n", dt, dt2, l);

l = dt.until(dt2, ChronoUnit.HOURS);
System.out.printf("HOURS between %s and %s => %s%n", dt, dt2, l);

l = dt.until(dt2, ChronoUnit.MONTHS);
System.out.printf("MONTHS between %s and %s => %s%n", dt, dt2, l);

l = dt.until(dt2, ChronoUnit.YEARS);
System.out.printf("YEARS between %s and %s => %s%n", dt, dt2, l);
}


}

Output

DAYS between 1999-01-20T20:10:30 and 2005-10-01T10:05 => 2445
HOURS between 1999-01-20T20:10:30 and 2005-10-01T10:05 => 58693
MONTHS between 1999-01-20T20:10:30 and 2005-10-01T10:05 => 80
YEARS between 1999-01-20T20:10:30 and 2005-10-01T10:05 => 6




See Also