Java Date and Time

Last update: 2026-06-01 17:26:29 +0800 +0800
tags: Date-Time | Java | Jdk
Available translations: 日本語 / 中文(简体) / 中文(繁體)

Datetime-related SDKs.

Time zone is an artificial concept, and only matters when a specific locale is concerned.

Contrary to intuition, the phrase local time means timezone-irrelevant, i.e. the class in question has no field storing timezone information.

Before Java 8: java.util.Date & Java.util.Calendar #

Java 8 and Later: java.time.* #

interface Temporal & final Class Instant Implements Temporal #

A Temporal instance represents an instant in an arbitrary time range. An amount of time can be added to this, subtracted from this, yet still produce a Temporal

The final class Instant implements Temporal by encapsulating a long for seconds since epoch and an int for nanosecond of that second. Notable methods:

  • static Instant now()
  • static Instant ofEpochMillis(long millis)
  • long until(Temporal endExclusive, TemporalUnit unit)

Uniting: interface TemporalUnit and enum ChronoUnit Implements TemporalUnit #

Each ChronoUnit enum instance is just an Duration with a descriptive name.

Time-Zone-Agnostic: java.time.LocalDate / java.time.LocalDateTime / java.time.LocalTime #

Local means time-zone agnostic.

Conceputally,

  • LocalDate is a (year-month-day) triplet,
  • LocalTime is a (hour-minute-second) triplet,
  • LocalDateTime is a (year-month-day-hour-minute-second) sextet.

Notable methods (on all 3 classes)

  • long until(Temporal endExclusive, TemporalUnit unit)

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

java.time.Clock #

java.time.DateTimeException #

java.time.DayOfWeek #

java.time.Duration #

java.time.Month #

java.time.MonthDay #

java.time.OffsetDateTime #

java.time.OffsetTime #

java.time.Period #

java.time.Ser #

java.time.Year #

java.time.YearMonth #

java.time.ZonedDateTime #

java.time.ZoneId #

java.time.ZoneOffset #

java.time.ZoneRegion #

Comments

Read or comment on GitHub (sign-in required to comment)