Languages
[Edit]
EN

Java 8 - DateTimeFormatter with ZonedDateTime.now() - how to get time zone offset with colon separator between hour and minute +hh:mm eg +02:00

13 points
Created by:
Root-ssh
175500

1. Problem description

How to get ZoneDateTime.now() formatted into:

2019-10-12 16:20 +02:00

+hh:mm - colon between hour and minutes? - how to achieve it?

Simple 'Z'  formats zone into '+0200' and we want to have '+02:00'

Current code with default ZoneDateTime.now() and zone 'Z'

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

public class ZoneDateTimeNowCurrentCode {

    public static void main(String[] args) {

        // 2019-10-12T16:20:28.008+02:00[Europe/Belgrade]
        System.out.println(ZonedDateTime.now());

        // 2019-10-12 16:20 +0200
        System.out.println(ZonedDateTime.now().format(DateTimeFormatter
                .ofPattern("yyyy-MM-dd HH:mm Z")));
    }
}

2. Solution

To get zone custom format into +hh:mm, instead of 'Z' we need to use '[XXX]' to get zone '+02:00'

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

public class ZoneDateTimeNowWithCustomZoneFormat {

    public static void main(String[] args) {

        // 2019-10-12 16:23 +02:00
        System.out.println(ZonedDateTime.now().format(DateTimeFormatter
                .ofPattern("yyyy-MM-dd HH:mm [XXX]")));
    }
}

3. Explanation of DateTimeFormatter patterns 'X', 'x' and 'Z'

From DateTimeFormatter java docs:

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 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
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join