Java LocalDateTime Methods

About This Index
This page is the Java LocalDateTime Methods index. Each row links to a full guide with syntax, parameters, return value, three runnable examples, and FAQs. Every example is a complete Java class with a main method and fixed data, and all of them compile on Java 8 and later.
New to LocalDateTime? It holds a date and a time, such as 2026-03-15T10:30, with no time zone. It comes from the java.time package, so you write import java.time.LocalDateTime; first. You create one with now(), of(), or parse(), and read it with methods such as getYear(). It keeps time down to the nanosecond. It is immutable, so plusDays() and withHour() return a new object, and the old one never changes.
Writing dt.plusDays(1); on its own does nothing, because the new date-time is thrown away. Always store it: dt = dt.plusDays(1);. Also remember that months are numbered from 1 (January) to 12, and that a LocalDateTime has no zone, so it is not an exact moment on the timeline. To get one, use atZone() or atOffset().
How to Call a LocalDateTime Method
Create a LocalDateTime, then call each method on the variable and keep the result:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30); // 2026-03-15T10:30
LocalDateTime later = start.plusDays(2).plusHours(3); // 2026-03-17T13:30
boolean after = later.isAfter(start); // true
int year = start.getYear(); // 2026
String text = later.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")); // "17/03/2026 13:30"
System.out.println(start); // 2026-03-15T10:30, unchanged Java LocalDateTime Methods Table
All method guides in this Java tutorial series (62 tutorials), grouped by purpose.
Create a date-time
6 methodsGet the current date and time, build one from numbers, read one from text, or convert it from an epoch time, an Instant, or another date-time object.
| Method | Description | Tutorial |
|---|---|---|
now() | Get the current date and time from the system clock. | Try it |
of() | Build a date-time from the year, month, day, hour, and minute. | Try it |
parse() | Read a date-time from text. | Try it |
ofEpochSecond() | Build a date-time from seconds counted since 1970. | Try it |
ofInstant() | Convert an Instant into the date-time of a time zone. | Try it |
from() | Convert another date-time object into a LocalDateTime. | Try it |
Read the parts
10 methodsGet the year, the month, the day, the weekday, the hour, the minute, the second, or the nanosecond as a plain number or an enum.
| Method | Description | Tutorial |
|---|---|---|
getYear() | Read the year as a number. | Try it |
getMonth() | Read the month as a Month enum such as MARCH. | Try it |
getMonthValue() | Read the month as a number from 1 to 12. | Try it |
getDayOfMonth() | Read the day of the month, from 1 to 31. | Try it |
getDayOfWeek() | Read the weekday as a DayOfWeek enum such as SUNDAY. | Try it |
getDayOfYear() | Read the day number inside the year, from 1 to 366. | Try it |
getHour() | Read the hour on a 24-hour clock, from 0 to 23. | Try it |
getMinute() | Read the minute of the hour, from 0 to 59. | Try it |
getSecond() | Read the second of the minute, from 0 to 59. | Try it |
getNano() | Read the fraction of the second, in nanoseconds. | Try it |
Add time
9 methodsMove a date-time forward by days, weeks, months, years, hours, minutes, seconds, or nanoseconds. Each method returns a new object.
| Method | Description | Tutorial |
|---|---|---|
plus() | Add an amount in any unit, or add a Duration or a Period. | Try it |
plusDays() | Return a copy that is a number of days later. | Try it |
plusWeeks() | Return a copy that is a number of weeks later. | Try it |
plusMonths() | Return a copy that is a number of months later. | Try it |
plusYears() | Return a copy that is a number of years later. | Try it |
plusHours() | Return a copy that is a number of hours later. | Try it |
plusMinutes() | Return a copy that is a number of minutes later. | Try it |
plusSeconds() | Return a copy that is a number of seconds later. | Try it |
plusNanos() | Return a copy that is a number of nanoseconds later. | Try it |
Subtract time
9 methodsMove a date-time backward by any unit. These are the mirror images of the plus methods, and they follow the same end-of-month rules.
| Method | Description | Tutorial |
|---|---|---|
minus() | Subtract an amount in any unit, or subtract a Duration or a Period. | Try it |
minusDays() | Return a copy that is a number of days earlier. | Try it |
minusWeeks() | Return a copy that is a number of weeks earlier. | Try it |
minusMonths() | Return a copy that is a number of months earlier. | Try it |
minusYears() | Return a copy that is a number of years earlier. | Try it |
minusHours() | Return a copy that is a number of hours earlier. | Try it |
minusMinutes() | Return a copy that is a number of minutes earlier. | Try it |
minusSeconds() | Return a copy that is a number of seconds earlier. | Try it |
minusNanos() | Return a copy that is a number of nanoseconds earlier. | Try it |
Change one part
10 methodsReplace a single part, such as the year, the month, the day, or the hour, or cut the small parts of the time away with truncatedTo().
| Method | Description | Tutorial |
|---|---|---|
with() | Return a copy changed by an adjuster, or with one field replaced. | Try it |
withYear() | Return a copy with a different year. | Try it |
withMonth() | Return a copy with a different month. | Try it |
withDayOfMonth() | Return a copy with a different day of the month. | Try it |
withDayOfYear() | Return a copy with a different day of the year. | Try it |
withHour() | Return a copy with a different hour. | Try it |
withMinute() | Return a copy with a different minute. | Try it |
withSecond() | Return a copy with a different second. | Try it |
withNano() | Return a copy with a different fraction of a second. | Try it |
truncatedTo() | Cut off the parts of the time that are smaller than a unit. | Try it |
Compare and measure
7 methodsFind out which date-time is earlier, check if two are equal, sort them, use them as keys, and count the complete days or hours between them.
| Method | Description | Tutorial |
|---|---|---|
isBefore() | Check if this date-time is earlier than another. | Try it |
isAfter() | Check if this date-time is later than another. | Try it |
isEqual() | Check if two date-times are at the same moment on the local timeline. | Try it |
equals() | Check if two objects are the same date and time. | Try it |
compareTo() | Compare two date-times and get a negative, zero, or positive number. | Try it |
hashCode() | Get a number that goes with equals, for use in hash-based collections. | Try it |
until() | Count the complete units between two date-times. | Try it |
Format and convert
6 methodsTurn a date-time into text, or split it into a date and a time. Add an offset or a time zone to get a real moment on the timeline.
| Method | Description | Tutorial |
|---|---|---|
format() | Turn a date-time into text using a pattern you choose. | Try it |
toString() | Get the date-time as ISO-8601 text such as 2026-03-15T10:30. | Try it |
toLocalDate() | Get only the date part, without the time. | Try it |
toLocalTime() | Get only the time of day, without the date. | Try it |
atOffset() | Attach a fixed offset from UTC, such as +05:30, to a date-time. | Try it |
atZone() | Attach a time zone to a date-time and get a ZonedDateTime. | Try it |
Fields and queries
5 methodsThe general methods that work with any field: ask for a field by name, test what is supported, find the allowed range, or run a query.
| Method | Description | Tutorial |
|---|---|---|
get() | Read any field of the date-time as an int. | Try it |
getLong() | Read any field of the date-time as a long. | Try it |
isSupported() | Check if a field or a unit can be used with this date-time. | Try it |
range() | Get the valid range of values of a field for this date-time. | Try it |
query() | Run a query on the date-time and get a result of any type. | Try it |
Frequently Asked Questions
Start with now()
Read the current date and time from the clock, the first thing most programs do with dates.
12 people found this page helpful
