Format time in Java

How Java formats time values with DateTimeFormatter: 24-hour and military-style output, 12-hour AM/PM, localized styles, and copy, parse and accessibility limits.

Formatting time in Java turns a time value, usually a LocalTime from the java.time package, into a string such as 14:05, 1405 or 2:05 PM. This review covers how that conversion works, which pattern fits which purpose, and where the resulting text can cause problems once it leaves your program. It relies on documented Java behavior and general principles. No destination-specific tests were supplied, and gaps are noted where they matter.

Is java.time formatting the right choice for your use?

For most new code, DateTimeFormatter with LocalTime is the right tool. Its main benefit is that formatters are immutable and thread-safe, according to the Javadoc, so one formatter can be shared across threads. The legacy SimpleDateFormat class is mutable and not thread-safe.

The key tradeoff is between fixed patterns and localized output. A fixed pattern like HH:mm produces the same characters everywhere, which makes it predictable and easy to parse. Localized styles adapt to the reader's conventions, but their exact characters can change with the locale and the JDK version.

Use a fixed, plain format such as ISO_LOCAL_TIME when another program will read, store, sort or compare the text. That includes logs, files, APIs and database fields. Reserve localized output for text a person reads on screen.

What formatting actually changes

Formatting creates a new string. It does not modify the LocalTime value, which stays a pure time of day with no date and no time zone. In this context, format means converting a value into characters according to a pattern or locale rules. It has nothing to do with fonts, styling, reversed text or mirrored display.

The basic examples below use a fixed time of 14:05:09. Each comment shows the string the formatter produces for that value.

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

LocalTime t = LocalTime.of(14, 5, 9);

t.format(DateTimeFormatter.ISO_LOCAL_TIME);                    // 14:05:09
t.format(DateTimeFormatter.ofPattern("HH:mm"));                 // 14:05
t.format(DateTimeFormatter.ofPattern("HHmm"));                  // 1405
t.format(DateTimeFormatter.ofPattern("h:mm a", Locale.US));     // 2:05 PM
t.format(DateTimeFormatter.ofPattern("hh:mm:ss a", Locale.US)); // 02:05:09 PM

Pattern letters are case-sensitive. These are the ones most relevant to time of day:

Letter | Meaning | Example for 14:05:09
H / HH | Hour of day, 0-23 | 14
h / hh | Clock hour, 1-12 | 2 / 02
k / kk | Clock hour of day, 1-24 | 14
m / mm | Minute | 5 / 05
s / ss | Second | 9 / 09
S | Fraction of second | 0
a | AM/PM marker, locale-dependent | PM
'text' | Literal text in single quotes | 'h' prints h

24-hour and military-style time

For 24-hour output, use HH:mm or HH:mm:ss. Military-style time is usually written without a colon, which corresponds to the pattern HHmm and runs from 0000 to 2359.

Avoid kk for this purpose. It counts hours from 1 to 24, so midnight appears as 24 instead of 00.

Neither HH:mm nor HHmm contains locale-dependent text. That makes both stable across environments and easy to parse back.

12-hour local time with AM/PM

For 12-hour time, use h:mm a, or hh:mm a if you want a leading zero. The a field is text, so its output depends on the locale.

ofPattern(String) without a locale uses the JVM's default format locale. The same code can therefore print different markers on different machines. If the output must read AM and PM, pass Locale.US explicitly.

Localized time styles

DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT) or MEDIUM lets locale data decide the layout. That data includes hour cycle, separators and day-period markers.

The LONG and FULL time styles generally need time-zone information. Applying them to a plain LocalTime fails with an exception. Use a ZonedDateTime instead if you need those styles.

import java.time.format.FormatStyle;

DateTimeFormatter shortUs =
    DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).withLocale(Locale.US);
t.format(shortUs); // 2:05 PM (separator character depends on JDK version; see below)

Common mistakes

  • Using MM (month) when you mean mm (minute).
  • Using hh without a, which hides whether a time is morning or afternoon.
  • Relying on the default locale for AM/PM text that other code will parse.
  • Adding date or zone fields, such as yyyy or z, to a pattern used with LocalTime. Formatting fails because LocalTime has no such fields.
  • Sharing a SimpleDateFormat across threads instead of using DateTimeFormatter.

Will the result stay readable and usable after copying or sharing?

Fixed numeric patterns such as HH:mm and ISO_LOCAL_TIME contain only digits and colons. They copy, paste, search and parse predictably.

Localized output is less predictable. Starting with JDK 20, Java's locale data follows a CLDR update that places a narrow no-break space (U+202F) before AM or PM in some English localized time formats. Earlier versions used a regular space. The text looks almost identical, but it is a different character.

That hidden difference has practical consequences:

  • A search for 2:05 PM typed with a regular space may not match.
  • Parsers written for a regular space, including those on older JDKs, may reject the string.
  • Tests that compare against a hard-coded expected string may fail after a JDK upgrade.

Parsing is also strict about the AM/PM marker. A parser built from h:mm a with Locale.US expects the marker in the locale's form. Accepting variants such as lowercase pm requires a case-insensitive formatter built with DateTimeFormatterBuilder.parseCaseInsensitive().

Accessibility depends on the output format and on the assistive technology, and no screen reader testing was supplied for this review. One risk is that compact military-style strings like 1405 could be announced as an ordinary number rather than a time. Whether that happens with a particular screen reader is unverified.

Where a person reads the time on screen, a localized or colon-separated format is the more cautious choice. If the time is shown in a web page, consider keeping a machine-readable ISO value alongside the human-readable string.

When a plain fixed format is preferable

Store and exchange times in a fixed format, with ISO_LOCAL_TIME as the safest default. Convert to a localized format only at the point of display, with an explicit locale. This keeps the stored value stable across JDK upgrades and machines while still giving readers a familiar layout.

Gaps in this review

No measured results were supplied for specific JDK builds, operating systems, editors, messaging apps or screen readers. Statements about localized output describe documented behavior rather than tests. If your output goes to a specific destination, check the real string there, including the separator characters and locale data of the JDK you deploy on.