Java : Temporal with Examples
Temporal (Java SE 21 & JDK 21) with Examples.
You will find code examples on most Temporal methods.
Summary
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)
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)
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)
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)
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 plus (TemporalAmount amount)
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
long until (Temporal endExclusive, TemporalUnit 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)
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)
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
Related posts
- Get the current time
- Basics of Date and Time
- Don't use the legacy Date and Calendar classes, use new APIs instead
- API Examples
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock
- Date
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Duration
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDate
- LocalDateTime
- LocalTime
- Month
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- YearMonth
- ZonedDateTime
- ZoneId
- ZoneOffset