HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

LocalDateTime parse()

By Lokesh Gupta | Filed Under: Java Date/Time

Java 8 LocalDateTime class represents a date without associated timezone information. Learn to convert a date in string to LocalDateTime object in Java 8.

1. String to LocalDateTime example – default and custom patterns

Java example to convert a string into LocalDateTime using LocalDateTime.parse() method.

//Default pattern

LocalDateTime today = LocalDateTime.parse("2019-03-27T10:15:30");
System.out.println(today);

//Custom pattern

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss a");
LocalDateTime dateTime = LocalDateTime.parse("2019-03-27 10:15:30 AM", formatter);
System.out.println(dateTime);

Program output.

2019-03-27T10:15:30
2019-03-27T10:15:30

2. DateTimeFormatter.ISO_LOCAL_DATE_TIME

The default date pattern is DateTimeFormatter.ISO_LOCAL_DATE_TIME which is yyyy-MM-ddThh:mm:ss.

The format consists of:

  • The ISO_LOCAL_DATE
  • The letter ‘T’. Parsing is case insensitive.
  • The ISO_LOCAL_TIME

ISO_LOCAL_DATE_TIME = ISO_LOCAL_DATE + ‘T’ + ISO_LOCAL_TIME

3. DateTimeFormatter with Locale

Sometimes we may have dates in specific locales such as french e.g. 29-Mar-2019 will be written in french as 29-Mars-2019. To parse such dates, use DateTimeFormatter withLocale() method to get the formatter in that locale and parse the dates.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MMMM-dd HH:mm:ss a")
                                        .withLocale(Locale.FRENCH);

LocalDateTime date = LocalDateTime.parse("2019-mai-29 10:15:30 AM", formatter);

System.out.println(date);

Program output.

2019-05-29T10:15:30

Drop me your questions related to string to localdatetime conversion in Java 8 – in comments.

Happy Learning !!

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Leave a Reply

This comment form is under antispam protection
This comment form is under antispam protection
  Subscribe  
Notify of

Search Tutorials

Java Date Time Tutorial

  • Java 8 – LocalDate
  • Java 8 – LocalTime
  • Java 8 – LocalDateTime
  • Java 8 – ZonedDateTime
  • Java 8 – DateTimeFormatter
  • Java 8 – Get Current Date
  • Java 8 – Get Current Timestamp
  • Java 8 – Compare Dates
  • Java 8 – Convert to EST Timezone
  • Java 8 – Elapsed Time
  • Java 8 – Add days to Date
  • Java 8 – Duration between dates
  • Java 8 – Period between dates
  • Java – Date
  • Java – Locale
  • Java – Get Current Locale
  • Java – Check leap year
  • Java – Parse String to Date
  • Java – Format Calendar
  • Java – 12 hours pattern
  • Java – Currency Formatting
  • Java – Timezone Conversion
  • Java – Strict Date Validation
  • XMLGregorianCalendar to Date

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz