Java LocalDateTime of() Method
Example
Create a fixed date and time:
import java.time.LocalDateTime;
public class Main {
public static void main(String[] args) {
LocalDateTime myDateTime = LocalDateTime.of(2026, 1, 15, 14, 30);
System.out.println(myDateTime);
}
}
Definition and Usage
The of() method creates a LocalDateTime object from a year, a month, a day, an hour and a minute, and optionally a second and a nanosecond (a billionth of a second).
You can also make it from a LocalDate and a LocalTime: LocalDateTime.of(myDate, myTime).
Syntax
One of the following:
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute)
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)
public static LocalDateTime of(LocalDate date, LocalTime time)
There are also versions that take a Month value, like Month.JANUARY, instead of an int month.
Parameter Values
| Parameter | Description |
|---|---|
| year | Required. The year, for example 2026. |
| month | Required. The month, from 1 to 12. |
| dayOfMonth | Required. The day of the month, from 1 to 31. |
| hour | Required. The hour, from 0 to 23. |
| minute | Required. The minute, from 0 to 59. |
| second | Optional. The second, from 0 to 59. |
| nanoOfSecond | Optional. The nanosecond, from 0 to 999999999. |
| date | Required in the last version. The date part, as a LocalDate. |
| time | Required in the last version. The time part, as a LocalTime. |
Technical Details
| Returns: | A LocalDateTime object with the given date and time. |
|---|---|
| Throws: | DateTimeException - if a value is out of range, or if the date does not exist (for example February 30). |
Related Pages
❮ Date and Time Methods