0% completed
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.
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:
java.time
are immutable and thread-safe.In this section, we have covered the methods of particular date and time classes.
Represents a date (year, month, day) without time and time zone information.
Method | Description |
---|---|
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. |
Represents a time (hour, minute, second, nanosecond) without date and time zone information.
Method | Description |
---|---|
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. |
Combines LocalDate
and LocalTime
to represent both date and time without time zone information.
Method | Description |
---|---|
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 . |
Represents a date-time with a time zone in the ISO-8601 calendar system.
Method | Description |
---|---|
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. |
Represents a point in time (timestamp) in UTC.
Method | Description |
---|---|
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. |
Below are simple and concise examples demonstrating the usage of each key date and time class within a single Java file.
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.
.....
.....
.....