Sub topic 14
Formatting and Parsing with DateTimeFormatter
Dates in a program are objects. People and files usually show dates as text, such as "05/11/2024" or "Nov 5, 2024". You often need to turn one into the other.
- Formatting turns a date object into text.
- Parsing reads text and builds a date object from it.
DateTimeFormatter is the class in java.time.format that does both. It holds a pattern, which is a short string of letters that describes how the text looks.
Pattern Letters
Each letter in a pattern stands for one part of the date or time. These are the most common:
| Letter | Meaning | Example |
|---|---|---|
| `yyyy` | Year | 2024 |
| `MM` | Month as two digits | 11 |
| `MMM` | Short month name | Nov |
| `dd` | Day of month, two digits | 05 |
| `d` | Day of month, no leading zero | 5 |
| `HH` | Hour, 0 to 23 | 14 |
| `mm` | Minute | 30 |
| `EEEE` | Day of week name | Tuesday |
Any other characters, such as /, -, , and spaces, appear in the text exactly as you type them.
A Worked Example
This program parses a date written day first, then prints it in a US style.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Formatting {
public static void main(String[] args) {
DateTimeFormatter input = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate date = LocalDate.parse("05/11/2024", input);
System.out.println(date); // 2024-11-05
DateTimeFormatter output = DateTimeFormatter.ofPattern("MMM d, yyyy", Locale.US);
System.out.println(date.format(output)); // Nov 5, 2024
}
}Here is what each step does:
ofPattern("dd/MM/yyyy")creates a formatter for day, month, then year.LocalDate.parse(text, input)reads the text using that pattern.- Printing
datedirectly uses the standard ISO format,2024-11-05. Locale.USis a locale, which is a setting for a language and region. It makes the month name come out in English as "Nov".date.format(output)produces the new text.
When Parsing Fails
If the text does not match the pattern, parse throws a DateTimeParseException. You can catch it and handle bad input.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
public class BadInput {
public static void main(String[] args) {
DateTimeFormatter input = DateTimeFormatter.ofPattern("dd/MM/yyyy");
try {
LocalDate.parse("2024-11-05", input);
} catch (DateTimeParseException e) {
System.out.println("Could not parse: " + e.getParsedString());
}
}
}This prints Could not parse: 2024-11-05, because the text uses dashes and puts the year first.
Exercise
What does LocalDate.of(2024, 3, 7).format(DateTimeFormatter.ofPattern("dd.MM.yyyy")) return?
Show answer
"07.03.2024". The dd and MM letters add a leading zero, and the dots are copied into the text as written.
Exercise
A program uses the pattern "yyyy-MM-dd HH:MM" to print a meeting time of 14:30 on 2024-11-05. What is wrong with this pattern?
Show answer
The last part uses MM, which means month, so the output shows "14:11" instead of "14:30". The minute needs lowercase mm, giving the pattern "yyyy-MM-dd HH:mm". It also needs a LocalDateTime, because a plain LocalDate has no hours or minutes.