Java LocalDateTime of() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

In Java, LocalDateTime.of(...) builds a LocalDateTime from values you choose. The most common version takes the year, month, day, hour, and minute as numbers.

Seconds and nanoseconds are optional extra arguments. You can also pass a Month enum instead of the month number, or pass a LocalDate and a LocalTime together.

A static method of java.time.LocalDateTime (since Java 8): call it on the class, as LocalDateTime.of(), with no object needed. Add import java.time.LocalDateTime; at the top of your file.

Syntax

Call the static method with the values you need. The month is 1 for January.

Java
LocalDateTime a = LocalDateTime.of(2026, 3, 15, 10, 30);                  // 2026-03-15T10:30
LocalDateTime b = LocalDateTime.of(2026, 3, 15, 10, 30, 45);              // with seconds
LocalDateTime c = LocalDateTime.of(2026, Month.MARCH, 15, 10, 30);        // with a Month enum
LocalDateTime d = LocalDateTime.of(LocalDate.of(2026, 3, 15), LocalTime.of(10, 30));
CallWhat it does
of(y, m, d, h, min)Builds a date-time with zero seconds and no fraction.
of(y, m, d, h, min, s)Adds the second.
of(y, m, d, h, min, s, nano)Adds the second and the nanosecond.
of(date, time)Joins a LocalDate and a LocalTime.

Every version also accepts a Month enum in place of the month number (except the one that takes a LocalDate).

Library declarations
Java
// java.time.LocalDateTime
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(int year, Month month, int dayOfMonth, int hour, int minute)
public static LocalDateTime of(LocalDate date, LocalTime time)

Parameters

ParameterDescription
year (int)The year, from -999999999 to 999999999.
month (int or Month)The month: 1 to 12, or a value such as Month.MARCH.
dayOfMonth (int)The day, from 1 up to the length of that month.
hour, minute (int)The hour (0 to 23) and the minute (0 to 59).
second, nanoOfSecond (int)Optional. The second (0 to 59) and the nanosecond (0 to 999,999,999).
date, timeA LocalDate and a LocalTime for the last version.

Return Value

Return ValueDescription
LocalDateTimeThe new date-time.

Compare with Similar Methods

Other ways to get a LocalDateTime:

MethodHow it differs
now()Reads the current date and time.
parse(text)Reads a date-time from text such as 2026-03-15T10:30.
ofEpochSecond(s, n, offset)Builds a date-time from seconds since 1970.

Java Examples

Example 1 — Create Date-Times from Numbers

Build a date-time with minutes only, with seconds, and with a fraction of a second.

Java
import java.time.LocalDateTime;

public class LdtOfDemo {
    public static void main(String[] args) {
        LocalDateTime a = LocalDateTime.of(2026, 3, 15, 10, 30);
        LocalDateTime b = LocalDateTime.of(2026, 3, 15, 10, 30, 45);
        LocalDateTime c = LocalDateTime.of(2026, 3, 15, 10, 30, 45, 123_000_000);

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }
}

How It Works

  1. Year, month, day, hour, minute. The first call gives 15 March 2026 at 10:30, with zero seconds.
  2. Add the seconds. The second call adds 45, so the text shows :45.
  3. Add the fraction. The third call adds 123,000,000 nanoseconds, which prints as .123.

Example 2 — Use a Month Enum or a LocalDate and LocalTime

Build the same moment in three different ways and compare them.

Java
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;

public class LdtOfParts {
    public static void main(String[] args) {
        LocalDateTime byNumber = LocalDateTime.of(2026, 3, 15, 10, 30);
        LocalDateTime byEnum = LocalDateTime.of(2026, Month.MARCH, 15, 10, 30);
        LocalDateTime byParts = LocalDateTime.of(LocalDate.of(2026, 3, 15), LocalTime.of(10, 30));

        System.out.println(byNumber);
        System.out.println(byEnum.equals(byNumber));
        System.out.println(byParts.equals(byNumber));
    }
}

How It Works

  1. By number. The month 3 means March.
  2. By enum. Month.MARCH is easier to read, and it cannot be mistyped as 13.
  3. By parts. A LocalDate and a LocalTime are joined into one object.
  4. All three agree. The two equals calls print true.

Example 3 — See Which Values Are Rejected

Try a date that does not exist, a month 13, an hour 24, and a leap day in a leap year.

Java
import java.time.DateTimeException;
import java.time.LocalDateTime;

public class LdtOfInvalid {
    public static void main(String[] args) {
        try {
            LocalDateTime.of(2026, 2, 30, 10, 0);
        } catch (DateTimeException e) {
            System.out.println("30 Feb      -> DateTimeException");
        }
        try {
            LocalDateTime.of(2026, 13, 1, 0, 0);
        } catch (DateTimeException e) {
            System.out.println("month 13    -> DateTimeException");
        }
        try {
            LocalDateTime.of(2026, 3, 15, 24, 0);
        } catch (DateTimeException e) {
            System.out.println("hour 24     -> DateTimeException");
        }
        System.out.println("29 Feb 2024 -> " + LocalDateTime.of(2024, 2, 29, 23, 59));
    }
}

How It Works

  1. No 30 February. February never has 30 days, so the call throws a DateTimeException.
  2. No month 13. Months go from 1 to 12.
  3. No hour 24. Hours go from 0 to 23, so the next day starts at hour 0.
  4. A real leap day. 2024 is a leap year, so 29 February 2024 is accepted and printed.

Frequently Asked Questions

Use LocalDateTime.of(2026, 3, 15, 10, 30) for 15 March 2026 at 10:30. Add a second and a nanosecond if you need them.
1. January is 1 and December is 12. You can also pass Month.JANUARY to be clear.
Use LocalDateTime.of(date, time), or date.atTime(time), or time.atDate(date).
Java throws a DateTimeException straight away. It never creates an impossible date.
Use LocalDateTime.of(2026, 3, 15, 0, 0), or LocalDate.of(2026, 3, 15).atStartOfDay().

Next: ofEpochSecond()

Build a date-time from seconds counted since 1970.

ofEpochSecond() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful