Examples · Java

Try java.time date formatting patterns

Was it MM or mm? Does z or XXX give the offset? The quickest way to find out is to format one moment with each pattern and look.

This query takes a fixed moment in New York, formats it with five patterns, shows the same instant in three other zones, and works out a Period between two dates.

The query

java-date-formatting.java · paste it into a Java tab and press F5, or run byterunner run java-date-formatting.java in a terminal

// java.time formatting and arithmetic, tried on one fixed moment.
var moment = ZonedDateTime.of(2026, 3, 8, 14, 30, 0, 0, ZoneId.of("America/New_York"));

record Formatted(String pattern, String result) {}
var patterns = List.of("yyyy-MM-dd", "dd/MM/yyyy HH:mm", "EEEE, MMMM d", "h:mm a z", "yyyy-MM-dd'T'HH:mm:ssXXX");
dump(patterns.stream()
    .map(p -> new Formatted(p, moment.format(DateTimeFormatter.ofPattern(p, Locale.US))))
    .toList(), "Patterns");

record InZone(String zone, String localTime) {}
dump(List.of("UTC", "Europe/London", "Asia/Tokyo").stream()
    .map(z -> new InZone(z, moment.withZoneSameInstant(ZoneId.of(z)).toLocalDateTime().toString()))
    .toList(), "The same moment elsewhere");

dump(Period.between(LocalDate.of(2026, 1, 15), moment.toLocalDate()).toString(), "Since January 15th");
dump(moment.minusDays(1).getOffset() + " -> " + moment.getOffset(), "Offset change (clocks went forward)");

What it shows

The output of that run, from byterunner run. In the app each table is a sortable grid.

Patterns
pattern                   result
------------------------  -------------------------
yyyy-MM-dd                2026-03-08
dd/MM/yyyy HH:mm          08/03/2026 14:30
EEEE, MMMM d              Sunday, March 8
h:mm a z                  2:30 PM EDT
yyyy-MM-dd'T'HH:mm:ssXXX  2026-03-08T14:30:00-04:00
(5 rows)
The same moment elsewhere
zone           localTime
-------------  ----------------
UTC            2026-03-08T18:30
Europe/London  2026-03-08T18:30
Asia/Tokyo     2026-03-09T03:30
(3 rows)
Since January 15th: P1M21D
Offset change (clocks went forward): -05:00 -> -04:00

The moment chosen is the day the US clocks went forward in 2026, so the offset a day earlier differs. A fixed date like this keeps the output the same each run; ZonedDateTime.now() works just as well.

Patterns are case-sensitive: MM is the month and mm the minute, HH a 24-hour hour and hh a 12-hour one. Passing a Locale keeps day and month names in the language you expect.

Download ByteRunner Free for Windows and Linux. Getting started

More examples