Close

Java Date Time - YearMonth.until() Examples

Java Date Time Java Java API 


Class:

java.time.YearMonth

java.lang.Objectjava.lang.Objectjava.time.YearMonthjava.time.YearMonthjava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.lang.ComparableComparablejava.io.SerializableSerializableLogicBig

Method:

public long until(Temporal endExclusive,
                  TemporalUnit unit)

Returns the amount of time until the specified temporal object in terms of the specified unit.


Examples


package com.logicbig.example.yearmonth;

import java.time.*;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;

public class UntilExample {

public static void main(String... args) {
YearMonth y = YearMonth.of(1980, 5);
System.out.println(y);

until(y, LocalDate.now());
until(y, LocalDateTime.now());
until(y, ZonedDateTime.now());
until(y, OffsetDateTime.now());
until(y, YearMonth.now());
}

private static void until(YearMonth y, Temporal temporal) {

System.out.printf("%s = %s%n", temporal.getClass().getSimpleName(), temporal);
for (ChronoUnit unit : ChronoUnit.values()) {
if (y.isSupported(unit)) {
long l = y.until(temporal, unit);
System.out.printf("%10s > %s%n", unit, l);
}
}
}
}

Output

1980-05
LocalDate = 2017-05-01
Months > 444
Years > 37
Decades > 3
Centuries > 0
Millennia > 0
Eras > 0
LocalDateTime = 2017-05-01T15:51:19.802
Months > 444
Years > 37
Decades > 3
Centuries > 0
Millennia > 0
Eras > 0
ZonedDateTime = 2017-05-01T15:51:19.803-05:00[America/Chicago]
Months > 444
Years > 37
Decades > 3
Centuries > 0
Millennia > 0
Eras > 0
OffsetDateTime = 2017-05-01T15:51:19.804-05:00
Months > 444
Years > 37
Decades > 3
Centuries > 0
Millennia > 0
Eras > 0
YearMonth = 2017-05
Months > 444
Years > 37
Decades > 3
Centuries > 0
Millennia > 0
Eras > 0




See Also