Pattern Symbols: Time Zones (z, Z, V, x, X)
You'll learn how to format and parse time zone information using specialized pattern symbols in DateTimeFormatter.
Understanding Zone Pattern Symbols
When working with ZonedDateTime or OffsetDateTime, you often need to display or parse time zone information. Java's date-time API provides five distinct pattern symbols for different zone representations:
The Five Zone Symbols
z - Time zone name (e.g., "PST", "Pacific Standard Time")
- Short form: 1-3 z's give abbreviated names
- Long form: 4 z's give full names
Z - Zone offset (e.g., "+0000", "-0800")
- Formats as ±HHmm or ±HH:MM:ss depending on pattern length
V - Time zone ID (e.g., "America/Los_Angeles")
- Always shows the full zone identifier
x - Zone offset without colon, omits zero (e.g., "-08", "+0530")
- ISO 8601 compatible
- Shows "Z" for UTC when using single
x
X - Like x, but shows literal "Z" for UTC (e.g., "Z", "-08", "+05:30")
- Uppercase version, more common in ISO formats
Practical Examples
ZonedDateTime zdt = ZonedDateTime.parse("2024-01-15T10:30:00-08:00[America/Los_Angeles]");
DateTimeFormatter f1 = DateTimeFormatter.ofPattern("HH:mm z");
// "10:30 PST"
DateTimeFormatter f2 = DateTimeFormatter.ofPattern("HH:mm zzzz");
// "10:30 Pacific Standard Time"
DateTimeFormatter f3 = DateTimeFormatter.ofPattern("HH:mm Z");
// "10:30 -0800"
DateTimeFormatter f4 = DateTimeFormatter.ofPattern("HH:mm VV");
// "10:30 America/Los_Angeles"
DateTimeFormatter f5 = DateTimeFormatter.ofPattern("HH:mm X");
// "10:30 -08"
Key Takeaway: Use z for human-readable zone names, V for zone IDs, Z or X for numeric offsets, and prefer X over x when you want "Z" to represent UTC in ISO 8601 formats.