Java LocalDateTime Class Methods

The LocalDateTime class, part of the java.time package introduced in Java 8, is a versatile and essential tool for representing both date and time without any time-zone information. It combines the functionalities of LocalDate and LocalTime, allowing you to handle dates and times seamlessly in your Java applications.

Key Points

  • Immutable and Thread-Safe: LocalDateTime objects cannot be changed once created, ensuring consistency and making them safe for use in multi-threaded environments.
  • Easy Instance Creation: You can easily create instances for the current date and time, specific date and time values, or parse them from strings.
  • Comprehensive API: The class provides a wide range of methods for manipulating and accessing date and time information, such as adding/subtracting dates and times, modifying specific fields, and comparing date-time values.
  • Formatting and Parsing: LocalDateTime can be formatted into strings and parsed from strings using DateTimeFormatter, supporting both ISO-8601 and custom formats.

In this post, we'll explore the various methods available in the LocalDateTime class, providing detailed descriptions and links to separate posts for each method to help you master date-time manipulation in Java.

Java LocalDateTime Methods

Method Description
compareTo() Compares this date-time to another date-time.
equals() Checks if this date-time is equal to another date-time.
format() Formats this date-time using the specified formatter.
from() Obtains an instance of LocalDateTime from a temporal object.
get() Gets the value of the specified field from this date-time as an int.
getDayOfMonth() Gets the day-of-month field.
getDayOfWeek() Gets the day-of-week field.
getDayOfYear() Gets the day-of-year field.
getHour() Gets the hour-of-day field.
getMinute() Gets the minute-of-hour field.
getMonth() Gets the month-of-year field.
getMonthValue() Gets the month-of-year field as an int.
getNano() Gets the nano-of-second field.
getSecond() Gets the second-of-minute field.
getYear() Gets the year field.
isBefore() Checks if this date-time is before the specified date-time.
isEqual() Checks if this date-time is equal to the specified date-time.
minus() Returns a copy of this date-time with the specified amount subtracted.
minusDays() Returns a copy of this date-time with the specified number of days subtracted.
minusMinutes() Returns a copy of this date-time with the specified number of minutes subtracted.
minusMonths() Returns a copy of this date-time with the specified number of months subtracted.
minusWeeks() Returns a copy of this date-time with the specified number of weeks subtracted.
now() Obtains the current date-time from the system clock in the default time-zone.
of() Obtains an instance of LocalDateTime from a year, month, day, hour, minute, second and nanosecond.
parse() Obtains an instance of LocalDateTime from a text string such as 2007-12-03T10:15:30.
plusDays() Returns a copy of this date-time with the specified number of days added.
plusMinutes() Returns a copy of this date-time with the specified number of minutes added.
plusSeconds() Returns a copy of this date-time with the specified number of seconds added.
plusYears() Returns a copy of this date-time with the specified number of years added.
toLocalDate() Gets the LocalDate part of this date-time.
toLocalTime() Gets the LocalTime part of this date-time.
withDayOfMonth() Returns a copy of this date-time with the day-of-month altered.
withDayOfYear() Returns a copy of this date-time with the day-of-year altered.
withHour() Returns a copy of this date-time with the hour-of-day altered.
withMonth() Returns a copy of this date-time with the month-of-year altered.
withSecond() Returns a copy of this date-time with the second-of-minute altered.
withYear() Returns a copy of this date-time with the year altered.

For more details, visit the Java Documentation.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top