Tips for Picking the Right Date and Time Class for Your Java Application

By Doug Lowe

The first order of business when developing a Java application that must work with dates or times (or both) is picking the Date-Time class to represent your date and time values. The java.time package defines ten distinct classes used to represent different types of times and dates.

Ten Date-Time Classes in java.time
Class What It Represents
LocalTime A time (hours, minutes, and seconds to nanosecond precision) without an associated time zone.
LocalDate A date (year, month, and day) without an associated time zone.
LocalDateTime A date and time without an associated time zone.
OffsetTime A time and an offset from UTC (Coordinated Universal Time, also known as Greenwich Mean Time), such as 12:30:00-8.00, which means the time is 12:30 with an offset of -8 hours from UTC.
OffsetDateTime A date and time with an offset value from UTC.
ZonedDateTime A date and time with an associated time zone, such as America/Los_Angeles.
MonthDay A month and day without an associated year. You can use a MonthDay object to represent a date such as a birthday, anniversary, or holiday.
YearMonth A year and month, such as December, 2015. No day, time, or time zone values are associated with the year and month.
Year A year, such as 2038. No month, day, time, or time zone values are associated with the year.
Instant A single point of time, represented internally as the number of nanoseconds that have elapsed since midnight, January 1, 1970. The value assumes a UTC/GMT time offset of 0.

Each of these classes has many different methods that let you create date and time objects, perform calculations on them, compare them, and convert them to strings that can be displayed and read by humans. You can find complete documentation of the methods for each of these classes online.