Java 8 - ZonedDateTime with DateTimeFormatter - custom zone format patterns with Z and X symbols
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.
xxxxxxxxxx
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:
xxxxxxxxxx
# 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
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; |
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
.