Parsing date and time

To create a LocalDateTime object from a string you can use the static LocalDateTime.parse() method. It takes a string and a DateTimeFormatter as parameter. The DateTimeFormatter is used to specify the date/time pattern.

String str = "1986-04-08 12:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);

Formatting date and time

To create a formatted string out a LocalDateTime object you can use the format() method.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.of(1986, Month.APRIL, 8, 12, 30);
String formattedDateTime = dateTime.format(formatter); // "1986-04-08 12:30"

Note that there are some commonly used date/time formats predefined as constants in DateTimeFormatter. For example: Using DateTimeFormatter.ISO_DATE_TIME to format the LocalDateTime instance from above would result in the string "1986-04-08T12:30:00".

The parse() and format() methods are available for all date/time related objects (e.g. LocalDate or ZonedDateTime)

Answer from micha on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › localdate-parse-method-in-java-with-examples
LocalDate parse() method in Java with Examples - GeeksforGeeks
April 23, 2023 - parse() method of a LocalDate class used to get an instance of LocalDate from a string such as '2018-10-23' passed as parameter.The string must have a valid date-time and is parsed using DateTimeFormatter.ISO_LOCAL_DATE.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › time › LocalDate.html
LocalDate (Java Platform SE 8 )
October 20, 2025 - Obtains an instance of LocalDate from a text string such as 2007-12-03. The string must represent a valid date and is parsed using DateTimeFormatter.ISO_LOCAL_DATE.
🌐
Medium
medium.com › @AlexanderObregon › javas-localdate-parse-method-explained-d2c2bb7322cb
Java’s LocalDate.parse() Method Explained | Medium
August 31, 2024 - For instance, you might try to parse a date with a primary format, and if that fails, attempt to parse it with an alternative format. This approach can be particularly useful when dealing with data from various sources where the date format might not be consistent. ... import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; public class DateParsingWithFallback { public static void main(String[] args) { String dateString = "14-08-2024"; // Primary format fails LocalDate date = parseDateWithFallback(dateString); if (date != null) { S
🌐
TutorialsPoint
tutorialspoint.com › home › javatime › java localdate parse example
java.time.LocalDate.parse() Method Example
September 1, 2008 - The java.time.LocalDate.parse(CharSequence text, DateTimeFormatter formatter) method obtains an instance of LocalDate from a text string using a specific formatter.
🌐
How to do in Java
howtodoinjava.com › home › java date time › convert string to localdate in java
Convert String to LocalDate in Java - Convert String to LocalDate in Java - HowToDoInJava
February 7, 2023 - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy"); LocalDate date = LocalDate.parse("29-Mar-2019", formatter);
🌐
Mkyong
mkyong.com › home › java8 › java 8 – how to convert string to localdate
Java 8 - How to convert String to LocalDate - Mkyong.com
February 4, 2020 - Here are a few Java examples of converting a String to the new Java 8 Date API – java.time.LocalDate · DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy"); String date = "16/08/2016"; //convert String to LocalDate LocalDate localDate = LocalDate.parse(date, formatter); The key is understand the DateTimeFormatter patterns ·
Top answer
1 of 12
790

Parsing date and time

To create a LocalDateTime object from a string you can use the static LocalDateTime.parse() method. It takes a string and a DateTimeFormatter as parameter. The DateTimeFormatter is used to specify the date/time pattern.

String str = "1986-04-08 12:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);

Formatting date and time

To create a formatted string out a LocalDateTime object you can use the format() method.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.of(1986, Month.APRIL, 8, 12, 30);
String formattedDateTime = dateTime.format(formatter); // "1986-04-08 12:30"

Note that there are some commonly used date/time formats predefined as constants in DateTimeFormatter. For example: Using DateTimeFormatter.ISO_DATE_TIME to format the LocalDateTime instance from above would result in the string "1986-04-08T12:30:00".

The parse() and format() methods are available for all date/time related objects (e.g. LocalDate or ZonedDateTime)

2 of 12
222

You can also use LocalDate.parse() or LocalDateTime.parse() on a String without providing it with a pattern, if the String is in ISO 8601 format.

For example,

String strDate = "2015-08-04";
LocalDate aLD = LocalDate.parse(strDate);
System.out.println("Date: " + aLD);

String strDatewithTime = "2015-08-04T10:11:30";
LocalDateTime aLDT = LocalDateTime.parse(strDatewithTime);
System.out.println("Date with Time: " + aLDT);

Output,

Date: 2015-08-04
Date with Time: 2015-08-04T10:11:30

And use DateTimeFormatter only if you have to deal with other date patterns.

For instance, in the following example, dd MMM uuuu represents the day of the month (two digits), three letters of the name of the month (Jan, Feb, Mar,...), and a four-digit year:

DateTimeFormatter dTF = DateTimeFormatter.ofPattern("dd MMM uuuu");
String anotherDate = "04 Aug 2015";
LocalDate lds = LocalDate.parse(anotherDate, dTF);
System.out.println(anotherDate + " parses to " + lds);

Output

04 Aug 2015 parses to 2015-08-04

also remember that the DateTimeFormatter object is bidirectional; it can both parse input and format output.

String strDate = "2015-08-04";
LocalDate aLD = LocalDate.parse(strDate);
DateTimeFormatter dTF = DateTimeFormatter.ofPattern("dd MMM uuuu");
System.out.println(aLD + " formats as " + dTF.format(aLD));

Output

2015-08-04 formats as 04 Aug 2015

(See complete list of Patterns for Formatting and Parsing DateFormatter.)

  Symbol  Meaning                     Presentation      Examples
  ------  -------                     ------------      -------
   G       era                         text              AD; Anno Domini; A
   u       year                        year              2004; 04
   y       year-of-era                 year              2004; 04
   D       day-of-year                 number            189
   M/L     month-of-year               number/text       7; 07; Jul; July; J
   d       day-of-month                number            10

   Q/q     quarter-of-year             number/text       3; 03; Q3; 3rd quarter
   Y       week-based-year             year              1996; 96
   w       week-of-week-based-year     number            27
   W       week-of-month               number            4
   E       day-of-week                 text              Tue; Tuesday; T
   e/c     localized day-of-week       number/text       2; 02; Tue; Tuesday; T
   F       week-of-month               number            3

   a       am-pm-of-day                text              PM
   h       clock-hour-of-am-pm (1-12)  number            12
   K       hour-of-am-pm (0-11)        number            0
   k       clock-hour-of-am-pm (1-24)  number            0

   H       hour-of-day (0-23)          number            0
   m       minute-of-hour              number            30
   s       second-of-minute            number            55
   S       fraction-of-second          fraction          978
   A       milli-of-day                number            1234
   n       nano-of-second              number            987654321
   N       nano-of-day                 number            1234000000

   V       time-zone ID                zone-id           America/Los_Angeles; Z; -08:30
   z       time-zone name              zone-name         Pacific Standard Time; PST
   O       localized zone-offset       offset-O          GMT+8; GMT+08:00; UTC-08:00;
   X       zone-offset 'Z' for zero    offset-X          Z; -08; -0830; -08:30; -083015; -08:30:15;
   x       zone-offset                 offset-x          +0000; -08; -0830; -08:30; -083015; -08:30:15;
   Z       zone-offset                 offset-Z          +0000; -0800; -08:00;

   p       pad next                    pad modifier      1

   '       escape for text             delimiter
   ''      single quote                literal           '
   [       optional section start
   ]       optional section end
   #       reserved for future use
   {       reserved for future use
   }       reserved for future use
🌐
LabEx
labex.io › tutorials › java-how-to-parse-a-date-string-into-a-localdate-object-414098
How to parse a date string into a LocalDate object | LabEx
April 24, 2023 - To work with LocalDate objects, you need to understand the different date formats that can be used to represent a date. Date formats in Java follow the SimpleDateFormat pattern, which allows you to specify the order and format of the date components (year, month, day, etc.). ... Understanding these date formats is crucial when parsing date strings into LocalDate objects, as the format must match the input string for the parsing to succeed.
Find elsewhere
🌐
Lokalise
lokalise.com › home › java localdate localization tutorial: step by step examples
Java LocalDate localization tutorial: step by step examples
December 19, 2025 - The LocalDate.parse(CharSequence text, DateTimeFormatter formatter) method helps our localization efforts by letting us additionally pass a DateTimeFormatter argument holding the localized format we need LocalDate to use when parsing the date string.
🌐
Java67
java67.com › 2016 › 10 › how-to-parse-string-to-localdate-in-Java8-DateTimeFormatter-Example.html
How to parse String to LocalDate in Java 8 - DateTimeFormatter Example | Java67
You can also check out the What's New in Java 8 course on Pluaralsightto to learn more about the Date and Time API of Java 8. 1) The SimpleDateFormat wasn't thread-safe but DateTimeFormatter is thread-safe, that's why you can safely share pre-defined format among clients. 2) If your date String contains only the date part then use LocalDate.parse(), if it contains only time part then use LocalTime.parse() and if contains both date and time part then use LocalDateTime.parse() method.
🌐
Groovy
docs.groovy-lang.org › latest › html › groovy-jdk › java › time › LocalDate.html
LocalDate (Groovy JDK enhancements)
LocalDate#parse(java.lang.CharSequence, java.time.format.DateTimeFormatter) Returns a LocalDate that is days days after this date. Parameters: days - the number of days to add · Returns: a LocalDate · Since: 2.5.0 · Returns a LocalDate one day before this date.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › time › LocalDate.html
LocalDate (Java SE 17 & JDK 17)
October 20, 2025 - Obtains an instance of LocalDate from a text string such as 2007-12-03. The string must represent a valid date and is parsed using DateTimeFormatter.ISO_LOCAL_DATE.
🌐
Codefinity
codefinity.com › courses › v2 › 1fb0a0db-4487-432a-a9ff-7c54c61bac87 › b8fb63e8-2ca1-4d66-9d2f-51b2d87e392b › 8b2bbbca-df2b-4412-a17a-327396049993
Learn Parsing Dates with LocalDate.parse() | Parsing Dates and Formatting Tables
Using LocalDate.parse() allows you to transform a properly formatted string into a LocalDate object, enabling safe and reliable date calculations and comparisons. ... 123456789101112 package com.example; import java.time.LocalDate; public class ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.time.localdate.parse
LocalDate.Parse Method (Java.Time) | Microsoft Learn
Parse(Java.Lang.ICharSequence? text); [<Android.Runtime.Register("parse", "(Ljava/lang/CharSequence;)Ljava/time/LocalDate;", "", ApiSince=26)>] static member Parse : Java.Lang.ICharSequence -> Java.Time.LocalDate · text · ICharSequence · LocalDate · Attributes ·
🌐
LabEx
labex.io › tutorials › java-java-localdate-parse-with-formatter-117836
Java Programming | Date Operations | LocalDate Class | LabEx
In Java programming language, LocalDate ... provided by LocalDate class is parse(). parse() method parses a given text string representing date and returns the corresponding LocalDate object....
🌐
Baeldung
baeldung.com › home › java › java dates › creating a localdate with values in java
Creating a LocalDate with Values in Java | Baeldung
January 8, 2024 - LocalDate date = LocalDate.parse("8-Jan-2020", DateTimeFormatter.ofPattern("d-MMM-yyyy")); In this article, we’ve seen all the variants of creating a LocalDate with values in Java.
🌐
Studytonight
studytonight.com › java-examples › java-localdate-parse-method
Java LocalDate parse() Method - Studytonight
January 20, 2026 - Here, we are using parse() method to parse a text sequence to get LocalDate instance. import java.time.LocalDate; public class DateDemo { public static void main(String[] args){ LocalDate localDate = LocalDate.parse("2015-12-10"); ...
🌐
Studytonight
studytonight.com › java-examples › java-localdate-parse-with-formatter
Java LocalDate parse() with Formatter - Studytonight
July 23, 2025 - Java LocalDate parse() method is used to get a localdate from the text date with a specified format.