How can I parse/format dates with LocalDateTime? (Java 8)
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
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.
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.
This is useful for logs, UI output, exports, and integration formats that are not ISO by default.
Understand Pattern Symbols
Common pattern letters include:
- '
yyyyfor year' - '
MMfor month number' - '
ddfor day of month' - '
HHfor hour in 24-hour time' - '
mmfor minutes' - '
ssfor 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.
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.parsedirectly for ISO local date-time strings. - Use
DateTimeFormatter.ofPatternfor custom formats. - Format output with
value.format(formatter). - Handle parse failures with
DateTimeParseException. - Switch to
OffsetDateTimeorZonedDateTimewhen time-zone information matters.
Related reading
- How can I pass a parameter to a Java Thread?
- How can I play sound in Java?
- How can I produce messages with Kafka 8.2 API in Java?
- How can I propagate exceptions between threads?
- How can I properly compare two Integers in Java?
- How can I provide different database configurations with Spring Boot?
- How can I read a large text file line by line using Java?
- How can I read all files in a folder from Java?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.