Languages
[Edit]
EN

Java 8 - How to Format ZonedDateTime - examples

10 points
Created by:
RomanaLittle
458

1. Overview

In this post we cover usage of ZonedDateTime class introduced in Java 8 with code examples.

2. ZonedDateTime with DateTimeFormatter

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

public class Example1 {

    public static void main(String[] args) {

        ZonedDateTime zonedDateTime = ZonedDateTime.now();

        String pattern = "yyyy-MM-dd HH:mm:ss Z";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        String format = zonedDateTime.format(formatter);

        System.out.println("ZonedDateTime.now: ");
        System.out.println(zonedDateTime);
        System.out.println("Formatted: ");
        System.out.println(format);
    }
}

Output:

ZonedDateTime.now: 
2019-10-13T14:26:40.869+02:00[Europe/Belgrade]
Formatted: 
2019-10-13 14:26:40 +0200

3. ZonedDateTime with DateTimeFormatter - zone - XXX eg +hh:mm

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

public class Example2 {

    public static void main(String[] args) {

        ZonedDateTime zonedDateTime = ZonedDateTime.now();

        String pattern = "yyyy-MM-dd HH:mm:ss.SSS XXX";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        String format = zonedDateTime.format(formatter);

        System.out.println("ZonedDateTime.now: ");
        System.out.println(zonedDateTime);
        System.out.println("Formatted: ");
        System.out.println(format);
    }
}

Output:

ZonedDateTime.now:
2019-10-13T14:27:00.347+02:00[Europe/Belgrade]
Formatted:
2019-10-13 14:27:00.347 +02:00

4. String to ZonedDateTime

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

public class Example3 {

    public static void main(String[] args) {

        String dateTime = "2019-10-13 14:16:33 +0200";

        String pattern = "yyyy-MM-dd HH:mm:ss Z";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTime, formatter);

        System.out.println("ZonedDateTime: ");
        System.out.println(zonedDateTime);
    }
}

Output:

ZonedDateTime: 
2019-10-13T14:16:33+02:00

5. String to ZonedDateTime with build-in formatter ISO_ZONED_DATE_TIME

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

public class Example4 {

    public static void main(String[] args) {

        String dateTime = "2019-10-13T14:16:33.205+02:00[Europe/Belgrade]";

        DateTimeFormatter formatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTime, formatter);

        System.out.println("ZonedDateTime: ");
        System.out.println(zonedDateTime);
    }
}

Output:

ZonedDateTime:
2019-10-13T14:16:33.205+02:00[Europe/Belgrade]

6. String to ZonedDateTime with build-in formatter ISO_OFFSET_DATE_TIME

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

public class Example5 {

    public static void main(String[] args) {

        String dateTime = "2019-10-13T12:19:26+02:00";

        DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTime, formatter);

        System.out.println("ZonedDateTime: ");
        System.out.println(zonedDateTime);
    }
}

Output:

ZonedDateTime:
2019-10-13T12:19:26+02:00

References

  1. ZonedDateTime - JavaDoc
  2. DateTimeFormatter - JavaDoc
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