Languages
[Edit]
EN

Java 8 - ZonedDateTime with DateTimeFormatter - custom zone format patterns with Z and X symbols

14 points
Created by:
Kate_C
2854

1. Overview

Java 8 introduced DateTimeFormatter.

With DateTimeFormatter we can control zone format with couple of symbols eg: V, z, O, X, x, Z.

Below we have practical code examples with symbol Z and X.

2. DateTimeFormatter code examples with symbol Z and X

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class ZoneDateTimeNowWithCustomZoneFormatExamples {

    public static void main(String[] args) {

        System.out.println("# Example 1");
        // 2019-10-12 17:07 +0200
        System.out.println(ZonedDateTime.now().format(DateTimeFormatter
                .ofPattern("yyyy-MM-dd HH:mm Z")));

        System.out.println("# Example 2");
        // 2019-10-12 17:07 +02
        System.out.println(ZonedDateTime.now().format(DateTimeFormatter
                .ofPattern("yyyy-MM-dd HH:mm [X]")));

        System.out.println("# Example 3");
        // 2019-10-12 17:07 +0200
        System.out.println(ZonedDateTime.now().format(DateTimeFormatter
                .ofPattern("yyyy-MM-dd HH:mm [XX]")));

        System.out.println("# Example 4");
        // 2019-10-12 17:07 +02:00
        System.out.println(ZonedDateTime.now().format(DateTimeFormatter
                .ofPattern("yyyy-MM-dd HH:mm [XXX]")));

        System.out.println("# Example 5");
        // 2019-10-12 17:07+02:00
        System.out.println(ZonedDateTime.now().format(DateTimeFormatter
                .ofPattern("yyyy-MM-dd HH:mm[XXX]")));

        System.out.println("# Example 6");
        // 2019-10-12 17:07 +02:00+02
        System.out.println( ZonedDateTime.now().format(DateTimeFormatter
                .ofPattern("yyyy-MM-dd HH:mm [XXX][X]")) );

        System.out.println("# Example 7");
        // 2019-10-12 17:07 +02+02:00
        System.out.println(ZonedDateTime.now().format(DateTimeFormatter
                .ofPattern("yyyy-MM-dd HH:mm [X][XXX]")));
    }
}

Output:

# Example 1
2019-10-12 17:07 +0200
# Example 2
2019-10-12 17:07 +02
# Example 3
2019-10-12 17:07 +0200
# Example 4
2019-10-12 17:07 +02:00
# Example 5
2019-10-12 17:07+02:00
# Example 6
2019-10-12 17:07 +02:00+02
# Example 7
2019-10-12 17:07 +02+02:00

3. DateTimeFormatter zone symbols V, z, O, X, x, Z

Symbol Meaning Presentation Examples
V time-zone ID zone-id America/Los_Angeles; Z; -08:30
z time-zone name zone-name Pacific Standard Time; PST
O localized zone-offset offset-O GMT+8; GMT+08:00; UTC-08:00;
X zone-offset 'Z' for zero offset-X Z; -08; -0830; -08:30; -083015; -08:30:15;
x zone-offset  offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15;
Z zone-offset offset-Z +0000; -0800; -08:00;

4. DateTimeFormatter zone symbols V, z, O, X, x, Z - explanation

ZoneId: This outputs the time-zone ID, such as 'Europe/Paris'. If the count of letters is two, then the time-zone ID is output. Any other count of letters throws IllegalArgumentException.

Zone names: This outputs the display name of the time-zone ID. If the count of letters is one, two or three, then the short name is output. If the count of letters is four, then the full name is output. Five or more letters throws IllegalArgumentException.

Offset X and x: This formats the offset based on the number of pattern letters. One letter outputs just the hour, such as '+01', unless the minute is non-zero in which case the minute is also output, such as '+0130'. Two letters outputs the hour and minute, without a colon, such as '+0130'. Three letters outputs the hour and minute, with a colon, such as '+01:30'. Four letters outputs the hour and minute and optional second, without a colon, such as '+013015'. Five letters outputs the hour and minute and optional second, with a colon, such as '+01:30:15'. Six or more letters throws IllegalArgumentException. Pattern letter 'X' (upper case) will output 'Z' when the offset to be output would be zero, whereas pattern letter 'x' (lower case) will output '+00', '+0000', or '+00:00'.

Offset O: This formats the localized offset based on the number of pattern letters. One letter outputs the short form of the localized offset, which is localized offset text, such as 'GMT', with hour without leading zero, optional 2-digit minute and second if non-zero, and colon, for example 'GMT+8'. Four letters outputs the full form, which is localized offset text, such as 'GMT, with 2-digit hour and minute field, optional second field if non-zero, and colon, for example 'GMT+08:00'. Any other count of letters throws IllegalArgumentException.

Offset Z: This formats the offset based on the number of pattern letters. One, two or three letters outputs the hour and minute, without a colon, such as '+0130'. The output will be '+0000' when the offset is zero. Four letters outputs the full form of localized offset, equivalent to four letters of Offset-O. The output will be the corresponding localized offset text if the offset is zero. Five letters outputs the hour, minute, with optional second if non-zero, with colon. It outputs 'Z' if the offset is zero. Six or more letters throws IllegalArgumentException.

References

  1. DateTimeFormatter - Java docs
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.