Java added a new LocalDateTime class to Java 8 to represent local date-time and handle related operations.
It represents date and time both the information without timezone.
The date and time are bound to the local timezone in which the Java code is running.
It is good to use the LocalDateTime class when we don't need time-zone with the date time.
It follows the standard ISO-8601 calendar system to generate date-time without a time zone.
It represents the date-time format as 2022-03-14T17:28:13.048999208.
LocalDateTime class is an immutable class. Adding days or hours does not change the original object but rather returns a copy of a new local date-time object.
This class does not store or represent a time zone, for that purpose, Java provides ZonedDateTime class.
Let's have a look at the syntax of this class.
public final class LocalDateTime extends Object implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable
key points:
It is part of java.time package.
It was added in Java 8.
It is immutable and thread-safe.
It represents the local date and time in Java.
It consists of date and time in a single object
It does not include timezone.
it follows the ISO-8601 calendar.
It provides utility methods so that we can perform all operations related to date and time.
It extends Object class and implements several interfaces including Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, and Serializable.
The structure or format of the localdatetime instance is: year-month-day(T)hours:minutes:seconds.nenoseconds.
Possible operations we can perform on this local date-time object.
and many more.
Let's explore its methods and fields:
Modifier and Type | Description |
---|---|
static LocalDateTime MAX | It represnts the maximum supported LocalDateTime value: +999999999-12-31T23:59:59.999999999. |
static LocalDateTime MIN | It represents the minimum supported LocalDateTime value: -999999999-01-01T00:00:00. |
These are the useful methods of this class.
Method | Description |
---|---|
Temporal adjustInto(Temporal temporal) |
It adjusts the specified temporal object to have the same date and time as this object. |
OffsetDateTime atOffset(ZoneOffset offset) |
It combines this date-time with an offset to create an OffsetDateTime. |
It combines this date-time with a time-zone to create a ZonedDateTime. |
|
It compares this date-time to another date-time. |
|
It checks if this date-time is equal to another date-time. |
|
It formats this date-time using the specified formatter. |
|
static LocalDateTime from(TemporalAccessor temporal) |
It obtains an instance of LocalDateTime from a temporal object. |
int get(TemporalField field) |
It gets the value of the specified field from this date-time as an int. |
It gets the day-of-month field. |
|
It gets the day-of-week field, which is an enum DayOfWeek. |
|
int getDayOfYear() |
It gets the day-of-year field. |
It gets the hour-of-day field. |
|
long getLong(TemporalField field) |
It gets the value of the specified field from this date-time as a long type. |
It gets the minute-of-hour field. |
|
It gets the month-of-year field using the Month enum. |
|
int getMonthValue() |
It gets the month-of-year field from 1 to 12. |
It gets the nano-of-second field. |
|
It gets the second-of-minute field. |
|
It gets the year field. |
|
int hashCode() |
It is used to get a hash code for this date-time. |
boolean isAfter(ChronoLocalDateTime<?> other) |
It check if this date-time is after the specified date-time. |
boolean isBefore(ChronoLocalDateTime<?> other) |
It checks if this date-time is before the specified date-time. |
boolean isEqual(ChronoLocalDateTime<?> other) |
It checks if this date-time is equal to the specified date-time. |
boolean isSupported(TemporalField field) |
It checks if the specified field is supported. |
boolean isSupported(TemporalUnit unit) |
It checks if the specified unit is supported. |
LocalDateTime minus(long amountToSubtract, TemporalUnit unit) |
It returns a copy of this date-time with the specified amount subtracted. |
LocalDateTime minus(TemporalAmount amountToSubtract) |
It returns a copy of this date-time with the specified amount subtracted. |
It returns a copy of this LocalDateTime with the specified number of days subtracted. |
|
It returns a copy of this LocalDateTime with the specified number of hours subtracted. |
|
It returns a copy of this LocalDateTime with the specified number of minutes subtracted. |
|
It returns a copy of this LocalDateTime with the specified number of months subtracted. |
|
It returns a copy of this LocalDateTime with the specified number of nanoseconds subtracted. |
|
It returns a copy of this LocalDateTime with the specified number of seconds subtracted. |
|
It returns a copy of this LocalDateTime with the specified number of weeks subtracted. |
|
It returns a copy of this LocalDateTime with the specified number of years subtracted. |
|
It obtains the current date-time from the system clock in the default time zone. |
|
static LocalDateTime now(Clock clock) |
It is used to obtain the current date-time from the specified clock. |
static LocalDateTime now(ZoneId zone) |
It is used to obtain the current date-time from the system clock in the specified time zone. |
static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute) |
It is used to obtain an instance of LocalDateTime from the year, month, day, hour, and minute, setting the second and nanoseconds to zero. |
static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) |
It is used to obtain an instance of LocalDateTime from the year, month, day, hour, minute, and second, setting the nanosecond to zero. |
static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) |
It is used to obtain an instance of LocalDateTime from the year, month, day, hour, minute, second, and nanosecond. |
static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute) |
It is used to obtain an instance of LocalDateTime from the year, month, day, hour, and minute, setting the second and nanoseconds to zero. |
static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second) |
It is used to obtain an instance of LocalDateTime from the year, month, day, hour, minute, and second, setting the nanosecond to zero. |
static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) |
It is used to obtain an instance of LocalDateTime from the year, month, day, hour, minute, second, and nanosecond. |
static LocalDateTime of(LocalDate date, LocalTime time) |
It is used to obtain an instance of LocalDateTime from a date and time. |
static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) |
It is used to obtain an instance of LocalDateTime using seconds from the epoch of 1970-01-01T00:00:00Z. |
static LocalDateTime ofInstant(Instant instant, ZoneId zone) |
It is used to obtain an instance of LocalDateTime from an Instant and zone ID. |
static LocalDateTime parse(CharSequence text) |
It is used to obtain an instance of LocalDateTime from a text string such as 2007-12-03T10:15:30. |
static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter) |
It is used to obtain an instance of LocalDateTime from a text string using a specific formatter. |
LocalDateTime plus(long amountToAdd, TemporalUnit unit) |
It returns a copy of this date-time with the specified amount added. |
LocalDateTime plus(TemporalAmount amountToAdd) |
It returns a copy of this date-time with the specified amount added. |
It returns a copy of this LocalDateTime with the specified number of days added. |
|
It returns a copy of this LocalDateTime with the specified number of hours added. |
|
It returns a copy of this LocalDateTime with the specified number of minutes added. |
|
It returns a copy of this LocalDateTime with the specified number of months added. |
|
It returns a copy of this LocalDateTime with the specified number of nanoseconds added. |
|
It returns a copy of this LocalDateTime with the specified number of seconds added. |
|
It returns a copy of this LocalDateTime with the specified number of weeks added. |
|
It returns a copy of this LocalDateTime with the specified number of years added. |
|
<R> R query(TemporalQuery<R> query) |
It is used to query this date-time using the specified query. |
ValueRange range(TemporalField field) |
It is used to get the range of valid values for the specified field. |
It is used to get the LocalDate part of this date-time. |
|
It is used to get the LocalTime part of this date-time. |
|
String toString() |
It is used to get this date-time as a String, such as 2007-12-03T10:15:30. |
LocalDateTime truncatedTo(TemporalUnit unit) |
It returns a copy of this LocalDateTime with the time truncated. |
long until(Temporal endExclusive, TemporalUnit unit) |
It calculates the amount of time until another date-time in terms of the specified unit. |
LocalDateTime with(TemporalAdjuster adjuster) |
It returns an adjusted copy of this date-time. |
LocalDateTime with(TemporalField field, long newValue) |
It returns a copy of this date-time with the specified field set to a new value. |
LocalDateTime withDayOfMonth(int dayOfMonth) |
It returns a copy of this LocalDateTime with the day-of-month altered. |
It returns a copy of this LocalDateTime with the day-of-year altered. |
|
It returns a copy of this LocalDateTime with the hour-of-day altered. |
|
It returns a copy of this LocalDateTime with the minute-of-hour altered. |
|
LocalDateTime withMonth(int month) |
It returns a copy of this LocalDateTime with the month-of-year altered. |
It returns a copy of this LocalDateTime with the nano-of-second altered. |
|
It returns a copy of this LocalDateTime with the second-of-minute altered. |
|
It returns a copy of this LocalDateTime with the year altered. |