LocalDateTime
LocalDateTime
Class
LocalDateTime
ClassThe LocalDateTime
class represents a date and time without a time zone. It combines a LocalDate
and a LocalTime
to represent a specific date and time of day.
Static Methods
now(): LocalDateTime
now(): LocalDateTime
Returns the current date and time.
parse(text: String): LocalDateTime
parse(text: String): LocalDateTime
Parses the input text to create a
LocalDateTime
instance.
of(year: Integer, month: Integer, dayOfMonth: Integer, hour: Integer, minute: Integer)
of(year: Integer, month: Integer, dayOfMonth: Integer, hour: Integer, minute: Integer)
Creates a
LocalDateTime
instance with the specified year, month, day, hour, and minute values.
Getter Methods
getYear(): Int
getYear(): Int
Gets the year field.
Returns the year as an
int
value.Example Usage:
getMonthValue()
getMonthValue()
Gets the month-of-year field from 1 to 12.
Returns the month as an
int
from 1 to 12.Example Usage:
getDayOfMonth()
getDayOfMonth()
Gets the day-of-month field.
Returns the day-of-month as an
int
from 1 to 31.Example Usage:
getDayOfYear()
getDayOfYear()
Gets the day-of-year field.
Returns the day-of-year as an
int
from 1 to 365 (or 366 in a leap year).Example Usage:
Date Manipulation Methods
minusYears(yearsToSubtract: Long)
minusYears(yearsToSubtract: Long)
Returns a new
LocalDateTime
with the specified number of years subtracted.Example Usage:
minusMonths(monthsToSubtract: Long)
minusMonths(monthsToSubtract: Long)
Returns a new
LocalDateTime
with the specified number of months subtracted.Example Usage:
minusWeeks(weeksToSubtract: Long)
minusWeeks(weeksToSubtract: Long)
Returns a new
LocalDateTime
with the specified number of weeks subtracted.Example Usage:
minusDays(daysToSubtract: Long)
minusDays(daysToSubtract: Long)
Returns a new
LocalDateTime
with the specified number of days subtracted.Example Usage:
plusDays(daysToAdd: Long)
plusDays(daysToAdd: Long)
Returns a new
LocalDateTime
with the specified number of days added.Example Usage:
daysUntil(endDateTime: LocalDateTime)
daysUntil(endDateTime: LocalDateTime)
Calculates the number of days until the specified end date and time.
Example Usage:
monthsUntil(endDateTime: LocalDateTime)
monthsUntil(endDateTime: LocalDateTime)
Calculates the number of months until the specified end date and time.
Example Usage:
Comparison Methods
isBefore(other: LocalDateTime)
isBefore(other: LocalDateTime)
Checks if the current date and time is before the specified date and time.
Returns
true
if the current date and time is beforeother
,false
otherwise.Example Usage:
isAfter(other: LocalDateTime)
isAfter(other: LocalDateTime)
Checks if the current date and time is after the specified date and time.
Returns
true
if the current date and time is afterother
,false
otherwise.Example Usage:
Date-Time Separation Methods
toLocalDate()
toLocalDate()
Gets the
LocalDate
part of this date-time.Returns a
LocalDate
with the same year, month, and day as this date-time.Example Usage:
toLocalTime()
toLocalTime()
Gets the
LocalTime
part of this date-time.Returns a
LocalTime
with the same hour, minute, second, and nanosecond as this date-time.Example Usage: