Java DateTimeFormatter
DateTimeFormatter parses and formats temporal values using predefined formatters or custom patterns. It works with the Java time API and is immutable and thread-safe.
DateTimeFormatter is the primary formatter for classes in `java.time`, such as `LocalDate`, `LocalDateTime`, `OffsetDateTime`, and `ZonedDateTime`. It converts temporal objects to text with `format()` and can convert text back to temporal objects with parsing methods such as `LocalDate.parse()`.
Java provides predefined formatters through constants such as `DateTimeFormatter.ISO_LOCAL_DATE` and `DateTimeFormatter.ISO_DATE_TIME`. These are useful when you want standard, interoperable representations without defining a pattern yourself.
For application-specific text, `DateTimeFormatter.ofPattern()` creates a formatter from pattern symbols. For example, `dd-MM-yyyy` represents a two-digit day, two-digit month, and four-digit year. Pattern letters have specific meanings, so confusing symbols such as `MM` and `mm` can produce incorrect results.
A formatter can also be configured with a `Locale` when textual fields depend on language or regional conventions. When parsing external input, prefer an explicit formatter rather than relying on an implicit or environment-dependent format.
DateTimeFormatter is immutable and thread-safe, so a configured formatter can safely be reused. This makes it suitable for shared constants instead of creating a new formatter for every request.
Key points
- Use `format()` to turn temporal values into text and parsing methods to turn text into temporal values.
- Use predefined ISO formatters when a standard representation is appropriate.
- Use `ofPattern()` for application-specific formats.
- Pattern symbols are case-sensitive; `MM` means month while `mm` means minute.
- `DateTimeFormatter` instances are immutable and thread-safe.
Interview questions
What is the difference between formatting and parsing?
Formatting converts a temporal object into a string, typically through `format()`. Parsing performs the reverse transformation, converting text into a temporal object such as `LocalDate`. The formatter's pattern or predefined rules must match the textual representation being parsed.
What is DateTimeFormatter used for?
DateTimeFormatter converts Java temporal values to text and parses text into temporal values. It works with the modern `java.time` API. You can use predefined formatters for standard representations or create custom ones with `ofPattern()`.
How do you create a custom date format?
Use `DateTimeFormatter.ofPattern()` with a pattern string. For example, `dd-MM-yyyy` represents day, month, and year in that order. The pattern is case-sensitive, so `MM` and `mm` have different meanings.
Is DateTimeFormatter thread-safe?
Yes. DateTimeFormatter instances are immutable and thread-safe. A configured formatter can therefore be safely reused by multiple threads. This differs from legacy mutable formatters such as `SimpleDateFormat`.
When would you use a predefined formatter instead of a custom pattern?
Use a predefined formatter when the required representation matches a standard such as ISO date or ISO date-time. This avoids unnecessary custom rules and makes the intended format clearer. A custom formatter is appropriate when an external interface or user-facing requirement specifies a different layout.