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.
var localdateTime = LocalDateTime.of(2023, 10, 24, 15, 30);
// LDTIME 2023-10-24T15:30:00 Static Methods
now(): LocalDateTime
now(): LocalDateTimeReturns the current date and time.
var currentDateTime = LocalDateTime.now();parse(text: String): LocalDateTime
parse(text: String): LocalDateTimeParses the input text to create a
LocalDateTimeinstance.
var parsedDateTime = LocalDateTime.parse("2023-10-24T15:30:45");of(year: Integer, month: Integer, dayOfMonth: Integer, hour: Integer, minute: Integer)
of(year: Integer, month: Integer, dayOfMonth: Integer, hour: Integer, minute: Integer)Creates a
LocalDateTimeinstance with the specified year, month, day, hour, and minute values.
var customDateTime = LocalDateTime.of(2023, 10, 24, 15, 30);Getter Methods
getYear(): Int
getYear(): IntGets the year field.
Returns the year as an
intvalue.Example Usage:
var year = customDateTime.getYear();
getMonthValue()
getMonthValue()Gets the month-of-year field from 1 to 12.
Returns the month as an
intfrom 1 to 12.Example Usage:
var month = customDateTime.getMonthValue();
getDayOfMonth()
getDayOfMonth()Gets the day-of-month field.
Returns the day-of-month as an
intfrom 1 to 31.Example Usage:
var dayOfMonth = customDateTime.getDayOfMonth();
getDayOfYear()
getDayOfYear()Gets the day-of-year field.
Returns the day-of-year as an
intfrom 1 to 365 (or 366 in a leap year).Example Usage:
var dayOfYear = customDateTime.getDayOfYear();
Date Manipulation Methods
minusYears(yearsToSubtract: Long)
minusYears(yearsToSubtract: Long)Returns a new
LocalDateTimewith the specified number of years subtracted.Example Usage:
var newDateTime = customDateTime.minusYears(1);
minusMonths(monthsToSubtract: Long)
minusMonths(monthsToSubtract: Long)Returns a new
LocalDateTimewith the specified number of months subtracted.Example Usage:
var newDateTime = customDateTime.minusMonths(2);
minusWeeks(weeksToSubtract: Long)
minusWeeks(weeksToSubtract: Long)Returns a new
LocalDateTimewith the specified number of weeks subtracted.Example Usage:
var newDateTime = customDateTime.minusWeeks(3);
minusDays(daysToSubtract: Long)
minusDays(daysToSubtract: Long)Returns a new
LocalDateTimewith the specified number of days subtracted.Example Usage:
var newDateTime = customDateTime.minusDays(10);
plusDays(daysToAdd: Long)
plusDays(daysToAdd: Long)Returns a new
LocalDateTimewith the specified number of days added.Example Usage:
var newDateTime = customDateTime.plusDays(5);
daysUntil(endDateTime: LocalDateTime)
daysUntil(endDateTime: LocalDateTime)Calculates the number of days until the specified end date and time.
Example Usage:
var daysUntil = customDateTime.daysUntil(endDateTime);
monthsUntil(endDateTime: LocalDateTime)
monthsUntil(endDateTime: LocalDateTime)Calculates the number of months until the specified end date and time.
Example Usage:
var monthsUntil = customDateTime.monthsUntil(endDateTime);
Comparison Methods
isBefore(other: LocalDateTime)
isBefore(other: LocalDateTime)Checks if the current date and time is before the specified date and time.
Returns
trueif the current date and time is beforeother,falseotherwise.Example Usage:
var isBefore = customDateTime.isBefore(parsedDateTime);
isAfter(other: LocalDateTime)
isAfter(other: LocalDateTime)Checks if the current date and time is after the specified date and time.
Returns
trueif the current date and time is afterother,falseotherwise.Example Usage:
var isAfter = customDateTime.isAfter(parsedDateTime);
Date-Time Separation Methods
toLocalDate()
toLocalDate()Gets the
LocalDatepart of this date-time.Returns a
LocalDatewith the same year, month, and day as this date-time.Example Usage:
var datePart = customDateTime.toLocalDate();
toLocalTime()
toLocalTime()Gets the
LocalTimepart of this date-time.Returns a
LocalTimewith the same hour, minute, second, and nanosecond as this date-time.Example Usage:
var timePart = customDateTime.toLocalTime();
Was this helpful?