Explain Codes LogoExplain Codes Logo

String to LocalDate

java
date-time-parsing
java-8
exception-handling
Anton ShumikhinbyAnton Shumikhin·Mar 7, 2025
TLDR

To convert String to LocalDate directly, use:

// Just like turning water into wine, we turn a string into a LocalDate. Magic! LocalDate localDate = LocalDate.parse("2023-01-25");

For non-standard formats, employ a DateTimeFormatter:

// Format schmormat. We can handle it. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM d, yyyy"); LocalDate localDate = LocalDate.parse("Jan 25, 2023", formatter);

Always validate your formatter pattern matches the structure of your date string.

Handling special date formats and locales

Dealing with Locale specific dates? We got you covered. Use a DateTimeFormatter with the correct Locale:

// A little friendly French date conversion for you DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d MMMM, yyyy").withLocale(Locale.FRENCH); LocalDate localDate = LocalDate.parse("25 janvier, 2023", formatter);

For those tricky date formats no one but you understands, the DateTimeFormatterBuilder is your friend. It allows you to build complex parsers equipped to handle optional elements:

// Here we've got a formatter that's got strong abdominal muscles and can flex to fit your needs DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd")) .appendOptional(DateTimeFormatter.ofPattern("dd/MM/yyyy")) .toFormatter();

Catching parsing exceptions. Because no one is perfect.

Always handle DateTimeParseException to avoid embarrassing crashes. Treat your exceptions like hot potatoes:

try { // Trying to perform a magic trick. Brace yourself. LocalDate localDate = LocalDate.parse(dateString, formatter); } catch (DateTimeParseException e) { // Oops! The rabbit is still in the hat. Treating the wound right away. // Perform exception-handling magic here. }

Java 8 vs Joda-Time: Dawn of Parsing

In the red corner, we have java.time classes like LocalDate, the reigning champs since Java 8. On the blue corner we've got the veteran contender, Joda Time:

// It's a bird...It's a plane...Oh wait, it's just a Joda LocalDateTime org.joda.time.format.DateTimeFormatter formatter = org.joda.time.format.DateTimeFormat.forPattern("yyyy-MM-dd"); org.joda.time.LocalDate jodaLocalDate = org.joda.time.LocalDate.parse("2025-01-25", formatter);

Both competitors are thread-safe and immutable. A real clash of the titans, and you're the victor regardless of the choice.

Unraveling the mysterious language of date-time patterns

For those perplexing times when you need to deal with dates written in ancient alien languages (also known as verbose date strings), here's a DateTimeFormatter custom pattern builder:

// A decoder ring for that cryptic, verbose date your ancient alien friend passed along DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E, MMM dd yyyy HH:mm:ss 'GMT'z", Locale.ENGLISH); LocalDate localDate = ZonedDateTime.parse("Tue, Mar 27 2025 14:15:30 GMT+0100", formatter).toLocalDate();