LocalDateTime

Stay organized with collections Save and categorize content based on your preferences.
public final class LocalDateTime extends Object
implements Temporal TemporalAdjuster ChronoLocalDateTime<LocalDate> Serializable

A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.

LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second. Other date and time fields, such as day-of-year, day-of-week and week-of-year, can also be accessed. Time is represented to nanosecond precision. For example, the value "2nd October 2007 at 13:45.30.123456789" can be stored in a LocalDateTime.

This class does not store or represent a time-zone. Instead, it is a description of the date, as used for birthdays, combined with the local time as seen on a wall clock. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.

The ISO-8601 calendar system is the modern civil calendar system used today in most of the world. It is equivalent to the proleptic Gregorian calendar system, in which today's rules for leap years are applied for all time. For most applications written today, the ISO-8601 rules are entirely suitable. However, any application that makes use of historical dates, and requires them to be accurate will find the ISO-8601 approach unsuitable.

Field Summary

public static final LocalDateTime MAX The maximum supported LocalDateTime, '+999999999-12-31T23:59:59.999999999'.
public static final LocalDateTime MIN The minimum supported LocalDateTime, '-999999999-01-01T00:00:00'.

Public Method Summary

Temporal
adjustInto(Temporal temporal)
Adjusts the specified temporal object to have the same date and time as this object.
OffsetDateTime
atOffset(ZoneOffset offset)
Combines this date-time with an offset to create an OffsetDateTime.
ZonedDateTime
atZone(ZoneId zone)
Combines this date-time with a time-zone to create a ZonedDateTime.
int
compareTo(ChronoLocalDateTime<?> other)
Compares this date-time to another date-time.
boolean
equals(Object obj)
Checks if this date-time is equal to another date-time.
String
format(DateTimeFormatter formatter)
Formats this date-time using the specified formatter.
static LocalDateTime
from(TemporalAccessor temporal)
Obtains an instance of LocalDateTime from a temporal object.
int
get(TemporalField field)
Gets the value of the specified field from this date-time as an int.
int
getDayOfMonth()
Gets the day-of-month field.
DayOfWeek
getDayOfWeek()
Gets the day-of-week field, which is an enum DayOfWeek.
int
getDayOfYear()
Gets the day-of-year field.
int
getHour()
Gets the hour-of-day field.
long
getLong(TemporalField field)
Gets the value of the specified field from this date-time as a long.
int
getMinute()
Gets the minute-of-hour field.
Month
getMonth()
Gets the month-of-year field using the Month enum.
int
getMonthValue()
Gets the month-of-year field from 1 to 12.
int
getNano()
Gets the nano-of-second field.
int
getSecond()
Gets the second-of-minute field.
int
getYear()
Gets the year field.
int
hashCode()
A hash code for this date-time.
boolean
isAfter(ChronoLocalDateTime<?> other)
Checks if this date-time is after the specified date-time.
boolean
isBefore(ChronoLocalDateTime<?> other)
Checks if this date-time is before the specified date-time.
boolean
isEqual(ChronoLocalDateTime<?> other)
Checks if this date-time is equal to the specified date-time.
boolean
isSupported(TemporalUnit unit)
Checks if the specified unit is supported.
boolean
isSupported(TemporalField field)
Checks if the specified field is supported.
LocalDateTime
minus(TemporalAmount amountToSubtract)
Returns a copy of this date-time with the specified amount subtracted.
LocalDateTime
minus(long amountToSubtract, TemporalUnit unit)
Returns a copy of this date-time with the specified amount subtracted.
LocalDateTime
minusDays(long days)
Returns a copy of this LocalDateTime with the specified number of days subtracted.
LocalDateTime
minusHours(long hours)
Returns a copy of this LocalDateTime with the specified number of hours subtracted.
LocalDateTime
minusMinutes(long minutes)
Returns a copy of this LocalDateTime with the specified number of minutes subtracted.
LocalDateTime
minusMonths(long months)
Returns a copy of this LocalDateTime with the specified number of months subtracted.
LocalDateTime
minusNanos(long nanos)
Returns a copy of this LocalDateTime with the specified number of nanoseconds subtracted.
LocalDateTime
minusSeconds(long seconds)
Returns a copy of this LocalDateTime with the specified number of seconds subtracted.
LocalDateTime
minusWeeks(long weeks)
Returns a copy of this LocalDateTime with the specified number of weeks subtracted.
LocalDateTime
minusYears(long years)
Returns a copy of this LocalDateTime with the specified number of years subtracted.
static LocalDateTime
now(ZoneId zone)
Obtains the current date-time from the system clock in the specified time-zone.
static LocalDateTime
now()
Obtains the current date-time from the system clock in the default time-zone.
static LocalDateTime
now(Clock clock)
Obtains the current date-time from the specified clock.
static LocalDateTime
of(int year, int month, int dayOfMonth, int hour, int minute, int second)
Obtains an instance of LocalDateTime from 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)
Obtains an instance of LocalDateTime from 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)
Obtains an instance of LocalDateTime from year, month, day, hour, minute, second and nanosecond.
static LocalDateTime
of(int year, int month, int dayOfMonth, int hour, int minute)
Obtains an instance of LocalDateTime from year, month, day, hour and minute, setting the second and nanosecond to zero.
static LocalDateTime
of(int year, Month month, int dayOfMonth, int hour, int minute)
Obtains an instance of LocalDateTime from year, month, day, hour and minute, setting the second and nanosecond to zero.
static LocalDateTime
of(LocalDate date, LocalTime time)
Obtains an instance of LocalDateTime from a date and time.
static LocalDateTime
of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)
Obtains an instance of LocalDateTime from year, month, day, hour, minute, second and nanosecond.
static LocalDateTime
ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset)
Obtains an instance of LocalDateTime using seconds from the epoch of 1970-01-01T00:00:00Z.
static LocalDateTime
ofInstant(Instant instant, ZoneId zone)
Obtains an instance of LocalDateTime from an Instant and zone ID.
static LocalDateTime
parse(CharSequence text, DateTimeFormatter formatter)
Obtains an instance of LocalDateTime from a text string using a specific formatter.
static LocalDateTime
parse(CharSequence text)
Obtains an instance of LocalDateTime from a text string such as 2007-12-03T10:15:30.
LocalDateTime
plus(TemporalAmount amountToAdd)
Returns a copy of this date-time with the specified amount added.
LocalDateTime
plus(long amountToAdd, TemporalUnit unit)
Returns a copy of this date-time with the specified amount added.
LocalDateTime
plusDays(long days)
Returns a copy of this LocalDateTime with the specified number of days added.
LocalDateTime
plusHours(long hours)
Returns a copy of this LocalDateTime with the specified number of hours added.
LocalDateTime
plusMinutes(long minutes)
Returns a copy of this LocalDateTime with the specified number of minutes added.
LocalDateTime
plusMonths(long months)
Returns a copy of this LocalDateTime with the specified number of months added.
LocalDateTime
plusNanos(long nanos)
Returns a copy of this LocalDateTime with the specified number of nanoseconds added.
LocalDateTime
plusSeconds(long seconds)
Returns a copy of this LocalDateTime with the specified number of seconds added.
LocalDateTime
plusWeeks(long weeks)
Returns a copy of this LocalDateTime with the specified number of weeks added.
LocalDateTime
plusYears(long years)
Returns a copy of this LocalDateTime with the specified number of years added.
<R> R
query(TemporalQuery<R> query)
Queries this date-time using the specified query.
ValueRange
range(TemporalField field)
Gets the range of valid values for the specified field.
LocalDate
toLocalDate()
Gets the LocalDate part of this date-time.
LocalTime
toLocalTime()
Gets the LocalTime part of this date-time.
String
toString()
Outputs this date-time as a String, such as 2007-12-03T10:15:30.
LocalDateTime
truncatedTo(TemporalUnit unit)
Returns a copy of this LocalDateTime with the time truncated.
long
until(Temporal endExclusive, TemporalUnit unit)
Calculates the amount of time until another date-time in terms of the specified unit.
LocalDateTime
with(TemporalField field, long newValue)
Returns a copy of this date-time with the specified field set to a new value.
LocalDateTime
with(TemporalAdjuster adjuster)
Returns an adjusted copy of this date-time.
LocalDateTime
withDayOfMonth(int dayOfMonth)
Returns a copy of this LocalDateTime with the day-of-month altered.
LocalDateTime
withDayOfYear(int dayOfYear)
Returns a copy of this LocalDateTime with the day-of-year altered.
LocalDateTime
withHour(int hour)
Returns a copy of this LocalDateTime with the hour-of-day altered.
LocalDateTime
withMinute(int minute)
Returns a copy of this LocalDateTime with the minute-of-hour altered.
LocalDateTime
withMonth(int month)
Returns a copy of this LocalDateTime with the month-of-year altered.
LocalDateTime
withNano(int nanoOfSecond)
Returns a copy of this LocalDateTime with the nano-of-second altered.
LocalDateTime
withSecond(int second)
Returns a copy of this LocalDateTime with the second-of-minute altered.
LocalDateTime
withYear(int year)
Returns a copy of this LocalDateTime with the year altered.

Inherited Method Summary

Fields

public static final LocalDateTime MAX

The maximum supported LocalDateTime, '+999999999-12-31T23:59:59.999999999'. This is the local date-time just before midnight at the end of the maximum date. This combines LocalDate.MAX and LocalTime.MAX. This could be used by an application as a "far future" date-time.

public static final LocalDateTime MIN

The minimum supported LocalDateTime, '-999999999-01-01T00:00:00'. This is the local date-time of midnight at the start of the minimum date. This combines LocalDate.MIN and LocalTime.MIN. This could be used by an application as a "far past" date-time.

Public Methods

public Temporal adjustInto (Temporal temporal)

Adjusts the specified temporal object to have the same date and time as this object.

This returns a temporal object of the same observable type as the input with the date and time changed to be the same as this.

The adjustment is equivalent to using Temporal.with(TemporalField, long) twice, passing ChronoField.EPOCH_DAY and ChronoField.NANO_OF_DAY as the fields.

In most cases, it is clearer to reverse the calling pattern by using Temporal.with(TemporalAdjuster):

   // these two lines are equivalent, but the second approach is recommended
   temporal = thisLocalDateTime.adjustInto(temporal);
   temporal = temporal.with(thisLocalDateTime);
 

This instance is immutable and unaffected by this method call.

Parameters
temporal the target object to be adjusted, not null
Returns
  • the adjusted object, not null
Throws
DateTimeException if unable to make the adjustment
ArithmeticException if numeric overflow occurs

public OffsetDateTime atOffset (ZoneOffset offset)

Combines this date-time with an offset to create an OffsetDateTime.

This returns an OffsetDateTime formed from this date-time at the specified offset. All possible combinations of date-time and offset are valid.

Parameters
offset the offset to combine with, not null
Returns
  • the offset date-time formed from this date-time and the specified offset, not null

public ZonedDateTime atZone (ZoneId zone)

Combines this date-time with a time-zone to create a ZonedDateTime.

This returns a ZonedDateTime formed from this date-time at the specified time-zone. The result will match this date-time as closely as possible. Time-zone rules, such as daylight savings, mean that not every local date-time is valid for the specified zone, thus the local date-time may be adjusted.

The local date-time is resolved to a single instant on the time-line. This is achieved by finding a valid offset from UTC/Greenwich for the local date-time as defined by the rules of the zone ID.

In most cases, there is only one valid offset for a local date-time. In the case of an overlap, where clocks are set back, there are two valid offsets. This method uses the earlier offset typically corresponding to "summer".

In the case of a gap, where clocks jump forward, there is no valid offset. Instead, the local date-time is adjusted to be later by the length of the gap. For a typical one hour daylight savings change, the local date-time will be moved one hour later into the offset typically corresponding to "summer".

To obtain the later offset during an overlap, call ZonedDateTime.withLaterOffsetAtOverlap() on the result of this method. To throw an exception when there is a gap or overlap, use ZonedDateTime.ofStrict(LocalDateTime, ZoneOffset, ZoneId).

Parameters
zone the time-zone to use, not null
Returns
  • the zoned date-time formed from this date-time, not null

public int compareTo (ChronoLocalDateTime<?> other)

Compares this date-time to another date-time.

The comparison is primarily based on the date-time, from earliest to latest. It is "consistent with equals", as defined by Comparable.

If all the date-times being compared are instances of LocalDateTime, then the comparison will be entirely based on the date-time. If some dates being compared are in different chronologies, then the chronology is also considered, see Comparable.compareTo(T).

Parameters
other the other date-time to compare to, not null
Returns
  • the comparator value, negative if less, positive if greater

public boolean equals (Object obj)

Checks if this date-time is equal to another date-time.

Compares this LocalDateTime with another ensuring that the date-time is the same. Only objects of type LocalDateTime are compared, other types return false.

Parameters
obj the object to check, null returns false
Returns
  • true if this is equal to the other date-time

public String format (DateTimeFormatter formatter)

Formats this date-time using the specified formatter.

This date-time will be passed to the formatter to produce a string.

Parameters
formatter the formatter to use, not null
Returns
  • the formatted date-time string, not null
Throws
DateTimeException if an error occurs during printing

public static LocalDateTime from (TemporalAccessor temporal)

Obtains an instance of LocalDateTime from a temporal object.

This obtains a local date-time based on the specified temporal. A TemporalAccessor represents an arbitrary set of date and time information, which this factory converts to an instance of LocalDateTime.

The conversion extracts and combines the LocalDate and the LocalTime from the temporal object. Implementations are permitted to perform optimizations such as accessing those fields that are equivalent to the relevant objects.

This method matches the signature of the functional interface TemporalQuery allowing it to be used as a query via method reference, LocalDateTime::from.

Parameters
temporal the temporal object to convert, not null
Returns
  • the local date-time, not null
Throws
DateTimeException if unable to convert to a LocalDateTime

public int get (TemporalField field)

Gets the value of the specified field from this date-time as an int.

This queries this date-time for the value of the specified field. The returned value will always be within the valid range of values for the field. If it is not possible to return the value, because the field is not supported or for some other reason, an exception is thrown.

If the field is a ChronoField then the query is implemented here. The supported fields will return valid values based on this date-time, except NANO_OF_DAY, MICRO_OF_DAY, EPOCH_DAY and PROLEPTIC_MONTH which are too large to fit in an int and throw a DateTimeException. All other ChronoField instances will throw an UnsupportedTemporalTypeException.

If the field is not a ChronoField, then the result of this method is obtained by invoking TemporalField.getFrom(TemporalAccessor) passing this as the argument. Whether the value can be obtained, and what the value represents, is determined by the field.

Parameters
field the field to get, not null
Returns
  • the value for the field
Throws
DateTimeException if a value for the field cannot be obtained or the value is outside the range of valid values for the field
UnsupportedTemporalTypeException if the field is not supported or the range of values exceeds an int
ArithmeticException if numeric overflow occurs

public int getDayOfMonth ()

Gets the day-of-month field.

This method returns the primitive int value for the day-of-month.

Returns
  • the day-of-month, from 1 to 31

public DayOfWeek getDayOfWeek ()

Gets the day-of-week field, which is an enum DayOfWeek.

This method returns the enum DayOfWeek for the day-of-week. This avoids confusion as to what int values mean. If you need access to the primitive int value then the enum provides the int value.

Additional information can be obtained from the DayOfWeek. This includes textual names of the values.

Returns
  • the day-of-week, not null

public int getDayOfYear ()

Gets the day-of-year field.

This method returns the primitive int value for the day-of-year.

Returns
  • the day-of-year, from 1 to 365, or 366 in a leap year

public int getHour ()

Gets the hour-of-day field.

Returns
  • the hour-of-day, from 0 to 23

public long getLong (TemporalField field)

Gets the value of the specified field from this date-time as a long.

This queries this date-time for the value of the specified field. If it is not possible to return the value, because the field is not supported or for some other reason, an exception is thrown.

If the field is a ChronoField then the query is implemented here. The supported fields will return valid values based on this date-time. All other ChronoField instances will throw an UnsupportedTemporalTypeException.

If the field is not a ChronoField, then the result of this method is obtained by invoking TemporalField.getFrom(TemporalAccessor) passing this as the argument. Whether the value can be obtained, and what the value represents, is determined by the field.

Parameters
field the field to get, not null
Returns
  • the value for the field
Throws
DateTimeException if a value for the field cannot be obtained
UnsupportedTemporalTypeException if the field is not supported
ArithmeticException if numeric overflow occurs

public int getMinute ()

Gets the minute-of-hour field.

Returns
  • the minute-of-hour, from 0 to 59

public Month getMonth ()

Gets the month-of-year field using the Month enum.

This method returns the enum Month for the month. This avoids confusion as to what int values mean. If you need access to the primitive int value then the enum provides the int value.

Returns
  • the month-of-year, not null
See Also

public int getMonthValue ()

Gets the month-of-year field from 1 to 12.

This method returns the month as an int from 1 to 12. Application code is frequently clearer if the enum Month is used by calling getMonth().

Returns
  • the month-of-year, from 1 to 12
See Also

public int getNano ()

Gets the nano-of-second field.

Returns
  • the nano-of-second, from 0 to 999,999,999

public int getSecond ()

Gets the second-of-minute field.

Returns
  • the second-of-minute, from 0 to 59

public int getYear ()

Gets the year field.

This method returns the primitive int value for the year.

The year returned by this method is proleptic as per get(YEAR). To obtain the year-of-era, use get(YEAR_OF_ERA).

Returns
  • the year, from MIN_YEAR to MAX_YEAR

public int hashCode ()

A hash code for this date-time.

Returns
  • a suitable hash code

public boolean isAfter (ChronoLocalDateTime<?> other)

Checks if this date-time is after the specified date-time.

This checks to see if this date-time represents a point on the local time-line after the other date-time.

   LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00);
   LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00);
   a.isAfter(b) == false
   a.isAfter(a) == false
   b.isAfter(a) == true
 

This method only considers the position of the two date-times on the local time-line. It does not take into account the chronology, or calendar system. This is different from the comparison in compareTo(ChronoLocalDateTime), but is the same approach as ChronoLocalDateTime.timeLineOrder().

Parameters
other the other date-time to compare to, not null
Returns
  • true if this date-time is after the specified date-time

public boolean isBefore (ChronoLocalDateTime<?> other)

Checks if this date-time is before the specified date-time.

This checks to see if this date-time represents a point on the local time-line before the other date-time.

   LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00);
   LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00);
   a.isBefore(b) == true
   a.isBefore(a) == false
   b.isBefore(a) == false
 

This method only considers the position of the two date-times on the local time-line. It does not take into account the chronology, or calendar system. This is different from the comparison in compareTo(ChronoLocalDateTime), but is the same approach as ChronoLocalDateTime.timeLineOrder().

Parameters
other the other date-time to compare to, not null
Returns
  • true if this date-time is before the specified date-time

public boolean isEqual (ChronoLocalDateTime<?> other)

Checks if this date-time is equal to the specified date-time.

This checks to see if this date-time represents the same point on the local time-line as the other date-time.

   LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00);
   LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00);
   a.isEqual(b) == false
   a.isEqual(a) == true
   b.isEqual(a) == false
 

This method only considers the position of the two date-times on the local time-line. It does not take into account the chronology, or calendar system. This is different from the comparison in compareTo(ChronoLocalDateTime), but is the same approach as ChronoLocalDateTime.timeLineOrder().

Parameters
other the other date-time to compare to, not null
Returns
  • true if this date-time is equal to the specified date-time

public boolean isSupported (TemporalUnit unit)

Checks if the specified unit is supported.

This checks if the specified unit can be added to, or subtracted from, this date-time. If false, then calling the plus(long, TemporalUnit) and minus methods will throw an exception.

If the unit is a ChronoUnit then the query is implemented here. The supported units are:

  • NANOS
  • MICROS
  • MILLIS
  • SECONDS
  • MINUTES
  • HOURS
  • HALF_DAYS
  • DAYS
  • WEEKS
  • MONTHS
  • YEARS
  • DECADES
  • CENTURIES
  • MILLENNIA
  • ERAS
All other ChronoUnit instances will return false.

If the unit is not a ChronoUnit, then the result of this method is obtained by invoking TemporalUnit.isSupportedBy(Temporal) passing this as the argument. Whether the unit is supported is determined by the unit.

Parameters
unit the unit to check, null returns false
Returns
  • true if the unit can be added/subtracted, false if not

public boolean isSupported (TemporalField field)

Checks if the specified field is supported.

This checks if this date-time can be queried for the specified field. If false, then calling the range, get and with(TemporalField, long) methods will throw an exception.

If the field is a ChronoField then the query is implemented here. The supported fields are:

  • NANO_OF_SECOND
  • NANO_OF_DAY
  • MICRO_OF_SECOND
  • MICRO_OF_DAY
  • MILLI_OF_SECOND
  • MILLI_OF_DAY
  • SECOND_OF_MINUTE
  • SECOND_OF_DAY
  • MINUTE_OF_HOUR
  • MINUTE_OF_DAY
  • HOUR_OF_AMPM
  • CLOCK_HOUR_OF_AMPM
  • HOUR_OF_DAY
  • CLOCK_HOUR_OF_DAY
  • AMPM_OF_DAY
  • DAY_OF_WEEK
  • ALIGNED_DAY_OF_WEEK_IN_MONTH
  • ALIGNED_DAY_OF_WEEK_IN_YEAR
  • DAY_OF_MONTH
  • DAY_OF_YEAR
  • EPOCH_DAY
  • ALIGNED_WEEK_OF_MONTH
  • ALIGNED_WEEK_OF_YEAR
  • MONTH_OF_YEAR
  • PROLEPTIC_MONTH
  • YEAR_OF_ERA
  • YEAR
  • ERA
All other ChronoField instances will return false.

If the field is not a ChronoField, then the result of this method is obtained by invoking TemporalField.isSupportedBy(TemporalAccessor) passing this as the argument. Whether the field is supported is determined by the field.

Parameters
field the field to check, null returns false
Returns
  • true if the field is supported on this date-time, false if not

public LocalDateTime minus (TemporalAmount amountToSubtract)

Returns a copy of this date-time with the specified amount subtracted.

This returns a