Java : TemporalAccessor with Examples

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


Summary

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

Class diagram

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

final var year = accessor.get(ChronoField.YEAR);
System.out.println(year); // 2100

final var month = accessor.get(ChronoField.MONTH_OF_YEAR);
System.out.println(month); // 4

final var day = accessor.get(ChronoField.DAY_OF_MONTH);
System.out.println(day); // 15
final TemporalAccessor accessor = LocalTime.of(10, 59);
System.out.println(accessor); // 10:59

final var hour = accessor.get(ChronoField.HOUR_OF_DAY);
System.out.println(hour); // 10

final var minute = accessor.get(ChronoField.MINUTE_OF_HOUR);
System.out.println(minute); // 59

Methods

default int get (TemporalField field)

Gets the value of the specified field as an int.

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

final var year = accessor.get(ChronoField.YEAR);
System.out.println(year); // 2100

final var month = accessor.get(ChronoField.MONTH_OF_YEAR);
System.out.println(month); // 4

final var day = accessor.get(ChronoField.DAY_OF_MONTH);
System.out.println(day); // 15
final TemporalAccessor accessor = LocalTime.of(10, 59);
System.out.println(accessor); // 10:59

final var hour = accessor.get(ChronoField.HOUR_OF_DAY);
System.out.println(hour); // 10

final var minute = accessor.get(ChronoField.MINUTE_OF_HOUR);
System.out.println(minute); // 59

long getLong (TemporalField field)

Gets the value of the specified field as a long.

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

final var year = accessor.getLong(ChronoField.YEAR);
System.out.println(year); // 2100

final var month = accessor.getLong(ChronoField.MONTH_OF_YEAR);
System.out.println(month); // 4

final var day = accessor.getLong(ChronoField.DAY_OF_MONTH);
System.out.println(day); // 15
final TemporalAccessor accessor = LocalTime.of(10, 59);
System.out.println(accessor); // 10:59

final var hour = accessor.getLong(ChronoField.HOUR_OF_DAY);
System.out.println(hour); // 10

final var minute = accessor.getLong(ChronoField.MINUTE_OF_HOUR);
System.out.println(minute); // 59

boolean isSupported (TemporalField field)

Checks if the specified field is supported.

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

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

System.out.println(accessor.isSupported(ChronoField.HOUR_OF_DAY)); // false

default <R> R query (TemporalQuery<R> query)

Queries this date-time.

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

final var ret = accessor.query(TemporalQueries.precision());
System.out.println(ret); // Days
final TemporalAccessor accessor = LocalTime.of(10, 59);
System.out.println(accessor); // 10:59

final var ret = accessor.query(TemporalQueries.precision());
System.out.println(ret); // Nanos

default ValueRange range (TemporalField field)

Gets the range of valid values for the specified field.

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

System.out.println(accessor.range(ChronoField.YEAR)); // -999999999 - 999999999
System.out.println(accessor.range(ChronoField.MONTH_OF_YEAR)); // 1 - 12
System.out.println(accessor.range(ChronoField.DAY_OF_MONTH)); // 1 - 30

Related posts

To top of page