Java : Temporal with Examples

Temporal (Java SE 20 & JDK 20) with Examples.
You will find code examples on most Temporal methods.


Summary

Framework-level interface defining read-write access to a temporal object, such as a date, time, offset or some combination of these.

Class diagram

final Temporal temporal = LocalDate.of(2100, 4, 10);
System.out.println(temporal); // 2100-04-10

final var ret1 = temporal.plus(Period.ofDays(5));
System.out.println(ret1); // 2100-04-15

final var ret2 = temporal.minus(Period.ofYears(101));
System.out.println(ret2); // 1999-04-10
final Temporal temporal = LocalTime.of(16, 59);
System.out.println(temporal); // 16:59

final var ret1 = temporal.plus(Duration.ofMinutes(1));
System.out.println(ret1); // 17:00

final var ret2 = temporal.minus(Duration.ofHours(10));
System.out.println(ret2); // 06:59

Methods

boolean isSupported (TemporalUnit unit)

Checks if the specified unit is supported.

final Temporal temporal = LocalDate.of(2100, 4, 15);
System.out.println(temporal); // 2100-04-15

System.out.println(temporal.isSupported(ChronoField.YEAR)); // true
System.out.println(temporal.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(temporal.isSupported(ChronoField.DAY_OF_MONTH)); // true

System.out.println(temporal.isSupported(ChronoField.HOUR_OF_DAY)); // false
final Temporal temporal = LocalTime.of(16, 30, 59);
System.out.println(temporal); // 16:30:59

System.out.println(temporal.isSupported(ChronoField.HOUR_OF_DAY)); // true
System.out.println(temporal.isSupported(ChronoField.MINUTE_OF_HOUR)); // true
System.out.println(temporal.isSupported(ChronoField.SECOND_OF_MINUTE)); // true

System.out.println(temporal.isSupported(ChronoField.YEAR)); // false

default Temporal minus (long amountToSubtract, TemporalUnit unit)

Returns an object of the same type as this object with the specified period subtracted.

final Temporal temporal = LocalDate.of(2100, 4, 10);
System.out.println(temporal); // 2100-04-10

final var ret1 = temporal.plus(5, ChronoUnit.DAYS);
System.out.println(ret1); // 2100-04-15

final var ret2 = temporal.minus(101, ChronoUnit.YEARS);
System.out.println(ret2); // 1999-04-10
final Temporal temporal = LocalTime.of(16, 59);
System.out.println(temporal); // 16:59

final var ret1 = temporal.plus(1, ChronoUnit.MINUTES);
System.out.println(ret1); // 17:00

final var ret2 = temporal.minus(10, ChronoUnit.HOURS);
System.out.println(ret2); // 06:59

default Temporal minus (TemporalAmount amount)

Returns an object of the same type as this object with an amount subtracted.

final Temporal temporal = LocalDate.of(2100, 4, 10);
System.out.println(temporal); // 2100-04-10

final var ret1 = temporal.plus(Period.ofDays(5));
System.out.println(ret1); // 2100-04-15

final var ret2 = temporal.minus(Period.ofYears(101));
System.out.println(ret2); // 1999-04-10
final Temporal temporal = LocalTime.of(16, 59);
System.out.println(temporal); // 16:59

final var ret1 = temporal.plus(Duration.ofMinutes(1));
System.out.println(ret1); // 17:00

final var ret2 = temporal.minus(Duration.ofHours(10));
System.out.println(ret2); // 06:59

Temporal plus (long amountToAdd, TemporalUnit unit)

Returns an object of the same type as this object with the specified period added.

Please see minus(long amountToSubtract, TemporalUnit unit).

default Temporal plus (TemporalAmount amount)

Returns an object of the same type as this object with an amount added.

Please see minus(TemporalAmount amount).

long until (Temporal endExclusive, TemporalUnit unit)

Calculates the amount of time until another temporal in terms of the specified unit.

final Temporal temporal1 = LocalTime.of(8, 0);
final Temporal temporal2 = LocalTime.of(10, 30);

System.out.println(temporal1); // 08:00
System.out.println(temporal2); // 10:30

System.out.println(temporal1.until(temporal2, ChronoUnit.HOURS)); // 2
System.out.println(temporal1.until(temporal2, ChronoUnit.MINUTES)); // 150

System.out.println(temporal2.until(temporal1, ChronoUnit.HOURS)); // -2
System.out.println(temporal2.until(temporal1, ChronoUnit.MINUTES)); // -150

default Temporal with (TemporalAdjuster adjuster)

Returns an adjusted object of the same type as this object with the adjustment made.

final Temporal temporal = LocalDate.of(2100, 4, 15);
System.out.println(temporal); // 2100-04-15

final var ret1 = temporal.with(Year.of(1999));
System.out.println(ret1); // 1999-04-15

final var ret2 = temporal.with(Month.JANUARY);
System.out.println(ret2); // 2100-01-15

final var ret3 = temporal.with(TemporalAdjusters.firstDayOfMonth());
System.out.println(ret3); // 2100-04-01

final var ret4 = temporal.with(TemporalAdjusters.lastDayOfMonth());
System.out.println(ret4); // 2100-04-30

Temporal with (TemporalField field, long newValue)

Returns an object of the same type as this object with the specified field altered.

final Temporal temporal = LocalDate.of(2100, 4, 15);
System.out.println(temporal); // 2100-04-15

final var ret1 = temporal.with(ChronoField.YEAR, 1999);
System.out.println(ret1); // 1999-04-15

final var ret2 = temporal.with(ChronoField.MONTH_OF_YEAR, 1);
System.out.println(ret2); // 2100-01-15

final var ret3 = temporal.with(ChronoField.DAY_OF_MONTH, 30);
System.out.println(ret3); // 2100-04-30

Methods declared in TemporalAccessor

get, getLong, isSupported, query, range

Please see the link below.


Related posts

To top of page