Java Date Format "hh24": Getting 24-Hour Time Right
Java has no hh24 pattern. That is Oracle SQL syntax. Learn what HH, hh, kk and KK do in Java, why hh24 gives misleading output, and when 24-hour time suits your use.
Many people search for "hh24" in Java because they know the notation from Oracle SQL, where TO_CHAR(date, 'HH24:MI') produces 24-hour time. Java does not use that syntax. In Java's date patterns, 24-hour time is written with uppercase HH. Typing hh24 into a Java pattern does not cause an error. It quietly produces the wrong output. This review explains what each hour pattern does, what goes wrong with hh24, and when a 24-hour format is the right choice for what you are building.
Short answer: use HH, not hh24
For 24-hour time in Java, use HH for hours (00 to 23) together with mm for minutes, for example "HH:mm" or "yyyy-MM-dd HH:mm:ss". This works the same way in the modern java.time.format.DateTimeFormatter and in the older java.text.SimpleDateFormat. The 24-hour form is the better choice for logs, data exchange, file names, sorting and international audiences, because it cannot be misread as AM or PM. For people reading times in a US interface, a 12-hour format with an AM/PM marker, or the locale's own formatting, is usually easier to read.
What the hour pattern letters actually change
A date pattern does not change the stored time value. A LocalDateTime or Date holds the same instant whatever pattern you use. The pattern only controls the text produced when that value is formatted, and the text accepted when a string is parsed. Java has four hour letters. They differ in range and in whether they are meant to be paired with an AM/PM marker.
| Pattern | Meaning | Range | Value at 15:05 | Value at 00:30 |
| HH | Hour of day (24-hour) | 00–23 | 15 | 00 |
| kk | Clock hour of day (24-hour, starts at 1) | 01–24 | 15 | 24 |
| hh | Clock hour of AM/PM (12-hour) | 01–12 | 03 | 12 |
| KK | Hour of AM/PM (12-hour, starts at 0) | 00–11 | 03 | 00 |
| a | AM/PM marker | AM or PM (locale text) | PM | AM |
Letter case matters across the whole pattern, not only for hours. MM means month and mm means minute. In DateTimeFormatter, yyyy is the year of era, uuuu is the proleptic year, and YYYY is the week-based year. Near the start or end of a calendar year, YYYY can give a different year than you expect. Most errors with 24-hour patterns come from mixing up case.
Why "hh24" gives misleading output instead of an error
In both SimpleDateFormat and DateTimeFormatter, ASCII letters are reserved as pattern symbols. Most other characters, digits included, are copied to the output as literal text. So Java reads "hh24" as the 12-hour field hh followed by the literal characters "24". At 3 PM it outputs "0324". At 3 AM it also outputs "0324". The result looks like a number, loses the AM/PM distinction, and can be mistaken for valid data.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
LocalDateTime t = LocalDateTime.of(2024, 3, 9, 15, 5);
// Oracle-style habit: wrong in Java
DateTimeFormatter wrong = DateTimeFormatter.ofPattern("hh24:mm");
System.out.println(t.format(wrong)); // 0324:05
// 24-hour time
DateTimeFormatter h24 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
System.out.println(t.format(h24)); // 2024-03-09 15:05
// 12-hour time with an explicit marker and locale
DateTimeFormatter h12 = DateTimeFormatter.ofPattern("hh:mm a", Locale.US);
System.out.println(t.format(h12)); // 03:05 PM
The commented outputs above follow from the documented pattern rules. They were not captured from an actual run, so check them in your own JDK before relying on exact text. This matters most for the AM/PM marker, whose text comes from locale data.
Translating Oracle and SQL habits
- Oracle HH24 → Java HH
- Oracle HH or HH12 → Java hh, paired with a
- Oracle MI (minutes) → Java mm
- Oracle MM (month) → Java MM
- Oracle SS → Java ss
- Oracle AM or PM → Java a
The minutes row causes the most trouble when converting. In Oracle, MM is month and MI is minute. In Java, MM is month and lowercase mm is minute. If you copy "HH24:MI" and change only the hour part to "HH:MI", the result is wrong. Java has no pattern letter I, and DateTimeFormatter rejects unknown letters with an exception.
Choosing between HH and kk
Both HH and kk produce 24-hour time. They differ only at midnight. HH shows midnight as 00, which matches ISO 8601 and what most systems expect. kk shows it as 24, which is rare and can break parsers or sorting. Unless a specific system requires a 1–24 clock, use HH.
Will the result stay readable and usable after copying or sharing?
Zero-padded 24-hour text such as "2024-03-09 15:05" contains only ASCII digits, hyphens, a space and a colon. It copies cleanly between editors, terminals, spreadsheets and chat tools. In year-month-day order it also sorts correctly as plain text. Its weak point is human readability: many US readers expect 12-hour time, and "15:05" can slow them down or confuse them.
Localized formats bring a copy-and-paste risk. If you use DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT) with Locale.US, the output follows the JDK's CLDR locale data and changes between JDK versions. From JDK 20 onward, the US short time format places a narrow no-break space (U+202F) before AM or PM instead of a regular space. The two look almost the same. However, a string a person retypes with an ordinary space may then fail to parse, and text-based search or comparison may not match. Never parse user-facing localized text as if it were a fixed machine format.
When you parse with hh, include the a field. A 12-hour value without a marker is ambiguous. DateTimeFormatter may refuse to resolve it to a LocalTime, and if you work around that, you risk silently mapping afternoon times to the morning.
Accessibility: no evidence was available on how particular screen readers announce 24-hour strings such as "15:05" compared with "3:05 PM". This review therefore makes no claim about any specific assistive technology. If spoken output matters for your interface, test it with the screen readers your users rely on. In HTML you can also provide the machine-readable value separately, for example in a time element's datetime attribute, while showing a localized form to readers.
Practical usability notes
- Prefer DateTimeFormatter in new code. It is immutable and thread-safe. SimpleDateFormat is not thread-safe and should not be shared between threads without synchronization.
- Pass an explicit Locale whenever the pattern contains text fields such as a, so that output does not depend on the server's default locale.
- For data exchange, consider the built-in ISO formatters, such as DateTimeFormatter.ISO_LOCAL_DATE_TIME, which already use 24-hour hours.
- If a value represents a real instant shared across regions, include an offset or zone (for example with OffsetDateTime and the XXX pattern), because 24-hour time alone does not say which time zone is meant.
When a 24-hour pattern is the right choice
Use HH-based 24-hour time when the text must be unambiguous, sortable and easy for machines to read: logs, APIs, CSV exports, file names, database interchange and international audiences. For text shown mainly to US readers, choose a 12-hour pattern with hh and a, or a localized formatter. If that text will also be stored or parsed, keep a separate ISO 8601 value for machines. Whichever you choose, do not use hh24 in Java. It is not an error, but it produces output that looks plausible and is wrong.
Gaps in this review
No subject-specific measurements or test results were provided. The behavior described here comes from the documented Java pattern rules. Example outputs are derived from those rules, not from recorded runs. Claims about screen reader announcements and about how particular applications handle the narrow no-break space were not tested and should be checked in your target environment.