import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.Map;
public class AllJavaDatePatterns {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
ZonedDateTime zoned = ZonedDateTime.now(ZoneId.systemDefault());
System.out.println("=== JAVA DateTimeFormatter PATTERNS ===\n");
// Helper: print all patterns in a category
printCategory("YEAR", Map.of(
"y", "Year (e.g., 2026)",
"yy", "2‑digit year",
"yyyy", "4‑digit year",
"Y", "Week‑based year",
"u", "Proleptic year"
), now);
printCategory("MONTH", Map.of(
"M", "Month number (1–12)",
"MM", "Month number with leading zero",
"MMM", "Short month name",
"MMMM", "Full month name",
"MMMMM", "Narrow month name"
), now);
printCategory("DAY", Map.of(
"d", "Day of month",
"dd", "Day of month (2 digits)",
"D", "Day of year",
"E", "Short weekday name",
"EEEE", "Full weekday name",
"EEEEE", "Narrow weekday name",
"e", "Localized weekday number",
"c", "Standalone weekday number"
), now);
printCategory("HOUR / MINUTE / SECOND", Map.of(
"H", "Hour (0–23)",
"HH", "Hour (00–23)",
"h", "Hour (1–12)",
"hh", "Hour (01–12)",
"m", "Minutes",
"mm", "Minutes (2 digits)",
"s", "Seconds",
"ss", "Seconds (2 digits)",
"S", "Fraction of second",
"SSS", "Milliseconds"
), now);
printCategory("AM/PM", Map.of(
"a", "AM/PM marker"
), now);
printCategory("TIME ZONE", Map.of(
"z", "General timezone name",
"Z", "RFC‑822 offset",
"X", "ISO‑8601 offset",
"XX", "ISO‑8601 offset (HHMM)",
"XXX", "ISO‑8601 offset (HH:MM)"
), zoned);
printCategory("WEEK", Map.of(
"w", "Week of year",
"W", "Week of month"
), now);
printCategory("QUARTER", Map.of(
"Q", "Quarter number",
"QQQ", "Quarter short name",
"QQQQ", "Quarter full name"
), now);
printCategory("ERA", Map.of(
"G", "Era (short)",
"GGGG", "Era (full)"
), now);
printCategory("FULL COMPOSITE FORMATS", Map.of(
"yyyy-MM-dd HH:mm:ss", "Standard timestamp",
"yyyy-MM-dd'T'HH:mm:ssXXX", "ISO‑8601 with timezone",
"EEE, dd MMM yyyy HH:mm:ss Z", "RFC‑1123"
), zoned);
System.out.println("\n=== END ===");
}
private static void printCategory(String title, Map<String, String> patterns, Object temporal) {
System.out.println("---- " + title + " ----");
for (Map.Entry<String, String> e : patterns.entrySet()) {
String pattern = e.getKey();
String desc = e.getValue();
try {
String formatted = DateTimeFormatter.ofPattern(pattern).format((temporal instanceof ZonedDateTime)
? (ZonedDateTime) temporal
: (LocalDateTime) temporal);
System.out.println(pattern + " — " + desc + ": " + formatted);
} catch (Exception ex) {
System.out.println(pattern + " — " + desc + ": (not supported)");
}
}
System.out.println();
}
}
/*
run:
=== JAVA DateTimeFormatter PATTERNS ===
---- YEAR ----
yy ? 2?digit year: 26
y ? Year (e.g., 2026): 2026
Y ? Week?based year: 2026
u ? Proleptic year: 2026
yyyy ? 4?digit year: 2026
---- MONTH ----
MMM ? Short month name: May
MMMM ? Full month name: May
M ? Month number (1?12): 5
MMMMM ? Narrow month name: M
MM ? Month number with leading zero: 05
---- DAY ----
D ? Day of year: 141
d ? Day of month: 21
c ? Standalone weekday number: 5
EEEE ? Full weekday name: Thursday
dd ? Day of month (2 digits): 21
e ? Localized weekday number: 5
EEEEE ? Narrow weekday name: T
E ? Short weekday name: Thu
---- HOUR / MINUTE / SECOND ----
h ? Hour (1?12): 8
HH ? Hour (00?23): 08
S ? Fraction of second: 9
ss ? Seconds (2 digits): 02
SSS ? Milliseconds: 944
s ? Seconds: 2
H ? Hour (0?23): 8
mm ? Minutes (2 digits): 16
m ? Minutes: 16
hh ? Hour (01?12): 08
---- AM/PM ----
a ? AM/PM marker: AM
---- TIME ZONE ----
z ? General timezone name: GMT
Z ? RFC?822 offset: +0000
X ? ISO?8601 offset: Z
XX ? ISO?8601 offset (HHMM): Z
XXX ? ISO?8601 offset (HH:MM): Z
---- WEEK ----
W ? Week of month: 4
w ? Week of year: 21
---- QUARTER ----
QQQQ ? Quarter full name: 2nd quarter
QQQ ? Quarter short name: Q2
Q ? Quarter number: 2
---- ERA ----
GGGG ? Era (full): Anno Domini
G ? Era (short): AD
---- FULL COMPOSITE FORMATS ----
EEE, dd MMM yyyy HH:mm:ss Z ? RFC?1123: Thu, 21 May 2026 08:16:02 +0000
yyyy-MM-dd'T'HH:mm:ssXXX ? ISO?8601 with timezone: 2026-05-21T08:16:02Z
yyyy-MM-dd HH:mm:ss ? Standard timestamp: 2026-05-21 08:16:02
*/