Java LocalDateTime parse() Method

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

Definition

In Java, LocalDateTime.parse(text) reads a LocalDateTime from a String written in the standard ISO form, such as 2026-03-15T10:30:45. The letter T separates the date from the time.

For any other text format, pass a DateTimeFormatter as the second argument, such as DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm").

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

Syntax

Pass the text, and a formatter if the text is not in ISO form.

Java
LocalDateTime a = LocalDateTime.parse("2026-03-15T10:30:45");             // ISO form
LocalDateTime b = LocalDateTime.parse("2026-03-15T10:30");                // seconds are optional

DateTimeFormatter f = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
LocalDateTime c = LocalDateTime.parse("15/03/2026 10:30", f);             // 2026-03-15T10:30
CallWhat it does
LocalDateTime.parse(text)Reads ISO text such as 2026-03-15T10:30.
LocalDateTime.parse(text, formatter)Reads text in the format described by the formatter.
dt.format(formatter)The reverse: turns a date-time into text.
LocalDate.parse(text)Reads only a date such as 2026-03-15.

The text must match the format exactly. Extra spaces, a space instead of T, or a missing time all fail.

Library declarations
Java
// java.time.LocalDateTime
public static LocalDateTime parse(CharSequence text)
public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)

Parameters

ParameterDescription
text (CharSequence)The text to read, such as a String.
formatter (DateTimeFormatter)Optional. Describes the format of the text. Without it, ISO format is used.

Return Value

Return ValueDescription
LocalDateTimeThe date-time that the text describes.

Compare with Similar Methods

Other ways to get a LocalDateTime:

MethodHow it differs
format(formatter)The opposite direction: date-time to text.
of(...)Builds a date-time from numbers, with no text.
from(temporal)Converts another date-time type, with no text.

Java Examples

Example 1 — Parse Standard ISO Text

Read two ISO strings, one with seconds and one without.

Java
import java.time.LocalDateTime;

public class LdtParseDemo {
    public static void main(String[] args) {
        LocalDateTime withSeconds = LocalDateTime.parse("2026-03-15T10:30:45");
        LocalDateTime noSeconds = LocalDateTime.parse("2026-03-15T10:30");

        System.out.println(withSeconds);
        System.out.println(noSeconds);
        System.out.println("year: " + noSeconds.getYear() + ", hour: " + noSeconds.getHour());
    }
}

How It Works

  1. The ISO form. Date, then T, then time, written as yyyy-MM-ddTHH:mm:ss.
  2. Seconds are optional. 10:30 is accepted, and the seconds become 0.
  3. A normal object. After parsing, you can call methods such as getYear() and getHour() on the result.

Example 2 — Parse with Your Own Pattern

Describe two other text formats with DateTimeFormatter.ofPattern and read them.

Java
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LdtParsePattern {
    public static void main(String[] args) {
        DateTimeFormatter european = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
        DateTimeFormatter withSpace = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        System.out.println(LocalDateTime.parse("15/03/2026 10:30", european));
        System.out.println(LocalDateTime.parse("2026-03-15 10:30:45", withSpace));
    }
}

How It Works

  1. A pattern describes the text. dd/MM/yyyy HH:mm means day, month, year, then hour and minute, with slashes and a space.
  2. Read day first. The text 15/03/2026 10:30 becomes 15 March 2026 at 10:30.
  3. A space instead of T. The second pattern uses a space, so 2026-03-15 10:30:45 is accepted here, but not by plain parse(text).

Example 3 — Handle Text That Cannot Be Parsed

Try a space instead of T, a date with no time, and a date that does not exist.

Java
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;

public class LdtParseBad {
    public static void main(String[] args) {
        String[] texts = {"2026-03-15 10:30", "2026-03-15", "2026-02-30T10:30"};

        for (String text : texts) {
            try {
                LocalDateTime.parse(text);
                System.out.println(text + " -> ok");
            } catch (DateTimeParseException e) {
                System.out.println(text + " -> DateTimeParseException");
            }
        }
    }
}

How It Works

  1. Three bad strings. Each one breaks a different rule.
  2. A space instead of T. The ISO form needs the letter T, so the first one fails.
  3. No time part. A LocalDateTime needs a time, so 2026-03-15 alone fails. Use LocalDate.parse for that.
  4. A date that does not exist. There is no 30 February, so the third one fails too, and the loop reports a DateTimeParseException each time.

Frequently Asked Questions

Use LocalDateTime.parse(text) for ISO text such as 2026-03-15T10:30, or LocalDateTime.parse(text, formatter) for another format.
The ISO format needs a T between the date and the time. Use a DateTimeFormatter with the pattern yyyy-MM-dd HH:mm, or replace the space with T.
MM is the month and mm is the minute. Mixing them up is a common bug.
HH is the 24-hour clock (0 to 23). hh is the 12-hour clock and needs an a for AM or PM.
Parse a LocalDate first, then call atStartOfDay(). For example LocalDate.parse("2026-03-15").atStartOfDay().

Next: plus()

Add an amount in any unit, or add a Duration or a Period.

plus() 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.

7 people found this page helpful