Top community members
All Wiki Articles Create Wiki Article

Many years me and my friends we were volunteers on my university doing programming classes for young people.

I got main conclusion: Young people are awesome, especially students, because they always bring new and fresh ideas :)
* I loved to do it but my time is limited

  I decided to create something that will help millions of students

Now I am here

co-founder

Java 8 - How to Format ZonedDateTime - examples

0 contributions
10 points

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
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more