Java
LocalDateTime
Date Parsing
Date Formatting
Programming

How can I parse/format dates with LocalDateTime? (Java 8)

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

LocalDateTime in Java 8 is the right choice when you need a date and time without a time-zone offset. Parsing and formatting both rely on DateTimeFormatter, and the main thing to remember is that your formatter pattern must exactly match the text you are reading or writing.

Parse ISO-Style Strings First

If the input already matches the standard ISO local date-time format, parsing is simple.

java
1import java.time.LocalDateTime;
2
3public class Demo {
4    public static void main(String[] args) {
5        LocalDateTime value = LocalDateTime.parse("2026-03-11T14:30:00");
6        System.out.println(value);
7    }
8}

This works because LocalDateTime.parse uses the ISO formatter by default.

Parse Custom Formats with DateTimeFormatter

When the input string uses a custom layout, define the pattern explicitly.

java
1import java.time.LocalDateTime;
2import java.time.format.DateTimeFormatter;
3
4DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
5LocalDateTime value = LocalDateTime.parse("2026/03/11 14:30:00", formatter);
6System.out.println(value);

The separators, field order, and zero-padding must match the input exactly or parsing will fail.

Format a LocalDateTime for Output

Formatting is the reverse operation: turn the object into a string using a formatter.

java
1import java.time.LocalDateTime;
2import java.time.format.DateTimeFormatter;
3
4LocalDateTime now = LocalDateTime.of(2026, 3, 11, 14, 30, 0);
5DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM yyyy HH:mm");
6
7String output = now.format(formatter);
8System.out.println(output);

This is useful for logs, UI output, exports, and integration formats that are not ISO by default.

Understand Pattern Symbols

Common pattern letters include:

  • 'yyyy for year'
  • 'MM for month number'
  • 'dd for day of month'
  • 'HH for hour in 24-hour time'
  • 'mm for minutes'
  • 'ss for seconds'

Mixing up MM and mm is a very common bug. One is month, the other is minutes.

Handle Parsing Failures Explicitly

Bad input should be treated as a normal possibility, not as an impossible edge case.

java
1import java.time.LocalDateTime;
2import java.time.format.DateTimeFormatter;
3import java.time.format.DateTimeParseException;
4
5DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
6
7try {
8    LocalDateTime parsed = LocalDateTime.parse("not-a-date", formatter);
9    System.out.println(parsed);
10} catch (DateTimeParseException ex) {
11    System.out.println("Invalid input: " + ex.getMessage());
12}

This matters whenever the source string comes from users, files, or external APIs.

Know When LocalDateTime Is the Wrong Type

LocalDateTime has no time-zone or offset information. If the string includes a zone such as Z or +02:00, use OffsetDateTime or ZonedDateTime instead.

That distinction is important because a date-time without zone information cannot represent an exact moment globally. It only represents local calendar fields.

Reuse Formatters Safely

DateTimeFormatter is immutable and thread-safe, so you can keep commonly used formatters in constants without the old SimpleDateFormat problems.

That makes Java 8 date/time code both cleaner and safer in concurrent applications.

Separate Parsing Rules from Display Rules

In many applications, the string format used for machine input is not the same as the format shown to users. Keeping those formatter choices separate reduces bugs and avoids accidentally feeding a UI-specific format back into a parser.

That separation also makes later localization changes much safer.

It improves maintainability too.

Common Pitfalls

  • Using a formatter pattern that does not exactly match the input string.
  • Mixing up month and minute pattern symbols.
  • Parsing zone-aware strings into LocalDateTime.
  • Treating user input as always valid and skipping error handling.
  • Recreating the same formatter repeatedly instead of reusing a constant.

Summary

  • Use LocalDateTime.parse directly for ISO local date-time strings.
  • Use DateTimeFormatter.ofPattern for custom formats.
  • Format output with value.format(formatter).
  • Handle parse failures with DateTimeParseException.
  • Switch to OffsetDateTime or ZonedDateTime when time-zone information matters.

Course illustration
Course illustration

All Rights Reserved.