Java : TemporalAccessor with Examples
TemporalAccessor (Java SE 21 & JDK 21) with Examples.
You will find code examples on most TemporalAccessor methods.
Summary
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)
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)
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)
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)
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)
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
- 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
- Temporal
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- YearMonth
- ZonedDateTime
- ZoneId
- ZoneOffset