Format Time in Java: What Formatting Changes and When It Stays Usable
How Java turns time values into text with DateTimeFormatter: pattern letters, locale styles, hidden characters, and copy, parse and accessibility limits.
Formatting a time in Java turns a temporal value, such as a LocalTime, into a string for people or other systems to read. The result can look simple, like 14:05 or 2:05 PM. Whether it stays usable depends on where it goes next: a screen, a log file, a spreadsheet, a parser, or another locale. This review covers what the transformation changes, where it helps, and where plain, fixed-format text is the safer choice.
Is formatting time in Java suitable for your use?
For display, java.time's DateTimeFormatter is the recommended tool. Its main benefit is control. You can choose a 24-hour or 12-hour clock with an explicit pattern, or let a locale's conventions decide through a localized style. The main tradeoff is that localized output is meant for people, not machines. Its exact characters can vary by locale and by JDK version. That makes it unreliable as input for parsing, comparison or storage.
- Use plain, fixed-format text, such as ISO 8601 output from DateTimeFormatter.ISO_LOCAL_TIME, instead of localized output when:
- the string will be stored, logged, or exchanged between systems
- another program will parse the value back
- values need to sort or compare correctly as text
- the same output must be byte-identical across JDK versions and machines
- the reader's locale is unknown and an unambiguous clock notation matters more than familiarity
What time formatting actually changes
Formatting does not alter the time value. A LocalTime of 14:05:30 stays the same object. Formatting produces a new string whose characters depend on the pattern or style and on the locale. In this context, "format" means arranging fields like hour, minute and AM/PM marker into text. It has nothing to do with fonts, character reversal, mirrored glyphs or Unicode look-alike substitution. Those are separate kinds of text transformation, and none of them apply here. The output is ordinary stored characters, not a visual effect. That is why invisible characters inside the output matter, as covered below.
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
LocalTime t = LocalTime.of(14, 5, 30);
DateTimeFormatter.ofPattern("HH:mm").format(t); // "14:05"
DateTimeFormatter.ofPattern("HH:mm:ss").format(t); // "14:05:30"
DateTimeFormatter.ofPattern("h:mm a", Locale.US).format(t); // "2:05 PM"
DateTimeFormatter.ISO_LOCAL_TIME.format(t); // "14:05:30"
In these examples, the expected outputs follow the documented pattern letters. The space in "h:mm a" is a literal from the pattern, so it is an ordinary space. The AM/PM text comes from the locale you pass in. The fractional-second part of ISO_LOCAL_TIME is printed only when the value has nonzero nanoseconds. These comments state documented behavior, not output captured from a specific test run.
| Letter | Meaning | Example for 14:05 |
| HH | hour of day, 0–23, two digits | 14 |
| H | hour of day, 0–23 | 14 |
| hh | clock hour, 1–12, two digits | 02 |
| h | clock hour, 1–12 | 2 |
| k | clock hour of day, 1–24 | 14 |
| K | hour of AM/PM, 0–11 | 2 |
| mm | minute of hour | 05 |
| ss | second of minute | 30 (for 14:05:30) |
| SSS | fraction of second, three digits | 000 |
| a | AM/PM marker, locale-dependent text | PM |
Most formatting bugs come from a few patterns. Uppercase MM is month and lowercase mm is minute, so "HH:MM" is a real bug. If a pattern uses h or hh without a, 2:05 in the afternoon is indistinguishable from 2:05 in the morning. If ofPattern is called without a Locale, text fields like the AM/PM marker use the JVM's default locale, so output can change from one server to the next. Finally, the pattern must match the value type. Asking a LocalTime for a time-zone field (z, VV or similar) throws an exception, because a LocalTime has no zone.
Localized styles and hidden characters
DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT) lets the locale's data choose the clock notation. For example, a US locale gives a 12-hour time with AM/PM, and a German locale gives a 24-hour time. That data comes from the Unicode CLDR project, and it changes with JDK upgrades. One documented change matters a lot in practice. Starting with JDK 20, which adopted CLDR 42, US English short times use a narrow no-break space (U+202F) before AM/PM instead of an ordinary space. The output looks the same on screen, but the stored characters are different. The longer styles (MEDIUM, LONG, FULL) can also require fields a LocalTime lacks. In many locales, LONG and FULL include a time zone and fail on zone-less values.
DateTimeFormatter shortUs =
DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).withLocale(Locale.US);
String s = shortUs.format(LocalTime.of(14, 5));
// JDK 20 and later: "2:05\u202FPM" (narrow no-break space)
// Earlier JDKs: "2:05 PM" (ordinary space)
// A pattern with an ordinary space will not accept the newer string:
DateTimeFormatter.ofPattern("h:mm a", Locale.US).parse(s); // DateTimeParseException on JDK 20+
Digits and text direction are separate questions. DateTimeFormatter prints ASCII digits by default, even for locales that conventionally use other numbering systems. Locale-specific digits require withDecimalStyle(DecimalStyle.of(locale)) or a locale-aware configuration such as localizedBy. When a time appears inside right-to-left text, its display order is handled by the Unicode bidirectional algorithm in the rendering environment, not by Java's formatter. If a formatted time appears reordered inside Arabic or Hebrew text, the problem is in the surrounding text or UI layer. The formatted string itself has not changed.
Will the formatted time stay usable after copying or sharing?
A formatted time is plain text, so it copies like any other string. Hidden characters travel with it. A string containing U+202F may fail to parse in Java code, to match a search for "2:05 PM" typed with an ordinary space, or to be recognized by a spreadsheet's time detection. Treat any of these destinations as unverified until tested in your own environment. One successful paste in one application does not show it works elsewhere. If text will be re-read by software, send ISO 8601 text alongside the display string, or instead of it.
For accessibility, a correctly formatted time is ordinary text, which assistive technology can read. Unlike mirrored or Unicode-styled text, formatting adds no decorative characters that would be announced character by character. However, no documentation or scoped test was supplied for how specific screen readers announce 24-hour times, compact 12-hour forms, or times containing a narrow no-break space. Test with the assistive technology your audience uses before relying on any particular reading. In web interfaces, an HTML time element with a machine-readable datetime attribute lets you show a localized string while keeping an unambiguous value available.
Search and sorting favor fixed formats. Zero-padded 24-hour text such as 09:05 and 14:05 sorts correctly as a string. Localized 12-hour text such as 9:05 AM and 2:05 PM does not. Localized forms also vary in AM/PM wording and spacing, which makes exact-match search and editing less predictable. Store and index the time value or its ISO form, and format only at the point of display.
Older Java code: SimpleDateFormat
Code written before Java 8 often uses java.text.SimpleDateFormat with java.util.Date. Its pattern letters look similar, but the class is mutable and not thread-safe, so a shared instance can produce corrupted output under concurrent use. DateTimeFormatter is immutable and thread-safe, and the java.time types separate local times from zoned ones, which prevents a class of time-zone mistakes. For new code, prefer java.time. When maintaining legacy code, convert at the boundary with methods such as Date.toInstant rather than mixing the two APIs throughout.
Limits of this review
- No measured test results were supplied; the code comments describe documented API behavior, not captured runs on a specific JDK build.
- The exact output of localized styles depends on the JDK version and its CLDR data; verify on the versions you deploy.
- No documented evidence was available for specific screen readers, spreadsheets or messaging apps handling formatted times; those destinations need your own scoped testing.
- Locale-specific behavior beyond the US English and German examples described here was not verified.