Formatting
LocalDateTime
always stores date-time in a standard way โ
but users want readable formats:
YYYY-MM-DD
MM/DD/YYYY
Friday, July 3rd, 2025 3:30 PM
This is where DateTimeFormatter
comes in.
Key concept:
DateTimeFormatter
formats a LocalDateTime
โ String.
It also parses Strings โ LocalDateTime
.
You can use built-in patterns or custom patterns.
Symbol | Meaning | Example |
---|---|---|
yyyy | Year | 2025 |
MM | Month (2 digits) | 07 |
dd | Day of month | 03 |
HH | Hour (24-hour) | 15 |
mm | Minutes | 30 |
ss | Seconds | 45 |
E | Day of week short | Thu |
EEEE | Day of week long | Thursday |
|
Below shows how to format LocalDateTime
in multiple ways.
Key points:
Use ofPattern()
with custom patterns.
format()
turns a date-time into a String.
You can adjust output for region, language, style.