LocalDateTime

LocalDateTime Class

The 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

  • Returns the current date and time.

var currentDateTime = LocalDateTime.now();

parse(text: String): LocalDateTime

  • Parses the input text to create a LocalDateTime instance.

var parsedDateTime = LocalDateTime.parse("2023-10-24T15:30:45");

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.

var customDateTime = LocalDateTime.of(2023, 10, 24, 15, 30);

Getter Methods

getYear(): Int

  • Gets the year field.

  • Returns the year as an int value.

  • Example Usage:

    var year = customDateTime.getYear();

getMonthValue()

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

  • Returns the month as an int from 1 to 12.

  • Example Usage:

    var month = customDateTime.getMonthValue();

getDayOfMonth()

  • Gets the day-of-month field.

  • Returns the day-of-month as an int from 1 to 31.

  • Example Usage:

    var dayOfMonth = customDateTime.getDayOfMonth();

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:

    var dayOfYear = customDateTime.getDayOfYear();

Date Manipulation Methods

minusYears(yearsToSubtract: Long)

  • Returns a new LocalDateTime with the specified number of years subtracted.

  • Example Usage:

    var newDateTime = customDateTime.minusYears(1);

minusMonths(monthsToSubtract: Long)

  • Returns a new LocalDateTime with the specified number of months subtracted.

  • Example Usage:

    var newDateTime = customDateTime.minusMonths(2);

minusWeeks(weeksToSubtract: Long)

  • Returns a new LocalDateTime with the specified number of weeks subtracted.

  • Example Usage:

    var newDateTime = customDateTime.minusWeeks(3);

minusDays(daysToSubtract: Long)

  • Returns a new LocalDateTime with the specified number of days subtracted.

  • Example Usage:

    var newDateTime = customDateTime.minusDays(10);

plusDays(daysToAdd: Long)

  • Returns a new LocalDateTime with the specified number of days added.

  • Example Usage:

    var newDateTime = customDateTime.plusDays(5);

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)

  • Calculates the number of months until the specified end date and time.

  • Example Usage:

    var monthsUntil = customDateTime.monthsUntil(endDateTime);

Comparison Methods

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 before other, false otherwise.

  • Example Usage:

    var isBefore = customDateTime.isBefore(parsedDateTime);

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 after other, false otherwise.

  • Example Usage:

    var isAfter = customDateTime.isAfter(parsedDateTime);

Date-Time Separation Methods

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:

    var datePart = customDateTime.toLocalDate();

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:

    var timePart = customDateTime.toLocalTime();

Last updated