Java Intermediate

0% completed

Previous
Next
Date and Time Classes in Java

Handling dates and times is a common requirement in software applications, whether it's scheduling events, logging activities, or managing time-sensitive data. Java provides a comprehensive set of classes within the java.time package (introduced in Java 8) to address these needs efficiently and accurately. These classes offer a modern and flexible approach to date and time manipulation, overcoming the limitations of older classes like java.util.Date and java.util.Calendar.

This lesson explores the key date and time classes in Java, their essential methods presented in tables, and simple examples demonstrating their usage. Understanding these classes enables developers to manage temporal data effectively, ensuring precision and reliability in their applications.

Overview of Java’s Date and Time API

Java's Date and Time API, housed in the java.time package, provides a robust framework for handling dates, times, time zones, and durations. It is designed to be immutable and thread-safe, ensuring that instances cannot be altered once created, which prevents common bugs related to mutable date and time objects.

Key Features:

  • Immutability: All classes in java.time are immutable and thread-safe.
  • Clarity and Precision: Clear separation between different aspects of date and time (e.g., date vs. time vs. date-time vs. instant).
  • Time Zone Support: Comprehensive support for time zones and offsets.
  • Fluent API: Methods are designed to be chained for more readable and concise code.

Key Date and Time Classes

In this section, we have covered the methods of particular date and time classes.

a. LocalDate

Represents a date (year, month, day) without time and time zone information.

MethodDescription
now()Obtains the current date from the system clock.
of(int year, Month month, int dayOfMonth)Creates an instance with specified year, month, and day.
getYear()Retrieves the year.
getMonth()Retrieves the month.
getDayOfMonth()Retrieves the day of the month.
plusDays(long days)Returns a copy with the specified number of days added.
minusDays(long days)Returns a copy with the specified number of days subtracted.
isLeapYear()Checks if the year is a leap year.

b. LocalTime

Represents a time (hour, minute, second, nanosecond) without date and time zone information.

MethodDescription
now()Obtains the current time from the system clock.
of(int hour, int minute, int second)Creates an instance with specified hour, minute, and second.
getHour()Retrieves the hour.
getMinute()Retrieves the minute.
getSecond()Retrieves the second.
plusHours(long hours)Returns a copy with the specified number of hours added.
minusMinutes(long minutes)Returns a copy with the specified number of minutes subtracted.
withSecond(int second)Returns a copy with the second-of-minute altered.

c. LocalDateTime

Combines LocalDate and LocalTime to represent both date and time without time zone information.

MethodDescription
now()Obtains the current date-time from the system clock.
of(LocalDate date, LocalTime time)Creates an instance with specified date and time.
getYear()Retrieves the year.
getMonth()Retrieves the month.
getDayOfMonth()Retrieves the day of the month.
getHour()Retrieves the hour.
getMinute()Retrieves the minute.
getSecond()Retrieves the second.
plusDays(long days)Returns a copy with the specified number of days added.
minusHours(long hours)Returns a copy with the specified number of hours subtracted.
toLocalDate()Converts to a LocalDate.
toLocalTime()Converts to a LocalTime.

d. ZonedDateTime

Represents a date-time with a time zone in the ISO-8601 calendar system.

MethodDescription
now()Obtains the current date-time with the system default time zone.
of(LocalDateTime dateTime, ZoneId zone)Creates an instance with specified date-time and time zone.
getZone()Retrieves the time zone.
withZoneSameInstant(ZoneId zone)Returns a copy with a different time zone, adjusting the instant accordingly.
toLocalDateTime()Converts to a LocalDateTime.
toInstant()Converts to an Instant.
plusDays(long days)Returns a copy with the specified number of days added.
minusMinutes(long minutes)Returns a copy with the specified number of minutes subtracted.

Instant

Represents a point in time (timestamp) in UTC.

MethodDescription
now()Obtains the current instant from the system clock.
ofEpochSecond(long epochSecond)Creates an instance representing the given epoch second.
getEpochSecond()Retrieves the epoch second.
plusSeconds(long seconds)Returns a copy with the specified number of seconds added.
minusMillis(long millis)Returns a copy with the specified number of milliseconds subtracted.
toEpochMilli()Converts to epoch milliseconds.
toString()Returns a string representation of the instant.

Examples: Using Date and Time Classes

Below are simple and concise examples demonstrating the usage of each key date and time class within a single Java file.

Example 1: Using LocalDate and LocalTime

Java
Java

. . . .

Example 2: Using LocalDateTime

Java
Java

. . . .

Java's Date and Time API, encapsulated within the java.time package, offers a modern, comprehensive, and efficient framework for handling temporal data. The key classes LocalDate, LocalTime, LocalDateTime, ZonedDateTime, Instant, Duration, and Period—provide specialized functionalities tailored to various aspects of date and time management.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next