Format ZonedDateTime in Java

In this tutorial we will learn how to format ZonedDateTime in Java.

Formatting means converting ZonedDateTime object into string.

Formatting ZonedDateTime object using predefined DateTimeFormatter

Before formatting let us create a ZonedDateTime object with default timezone.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import java.time.*;
import java.time.format.DateTimeFormatter;


public class ZonedDateTimeFormatandParse {


    public static void main(String[] args) {

        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        System.out.println(zonedDateTime);

    }
}
1
2023-12-02T20:41:31.980380300+05:30[Asia/Calcutta]

To format ZonedDateTime we need to call format method of instance object and pass DateTimeFormatter object.

We can use predefined DateTimeFormatter class for most of our use cases.

DateTimeFormatter will mention type of string representation of the ZonedDateTime we require.

Below table shows ISO format already available.

Formatter Description Example
BASIC_ISO_DATE Basic ISO date ‘20111203’
ISO_LOCAL_DATE ISO Local Date ‘2011-12-03’
ISO_OFFSET_DATE ISO Date with offset ‘2011-12-03+01:00’
ISO_DATE ISO Date with or without offset ‘2011-12-03+01:00’; ‘2011-12-03’
ISO_LOCAL_TIME Time without offset ‘10:15:30’
ISO_OFFSET_TIME Time with offset ‘10:15:30+01:00’
ISO_TIME Time with or without offset ‘10:15:30+01:00’; ‘10:15:30’
ISO_LOCAL_DATE_TIME ISO Local Date and Time ‘2011-12-03T10:15:30’
ISO_OFFSET_DATE_TIME Date Time with Offset ‘2011-12-03T10:15:30+01:00’
ISO_ZONED_DATE_TIME Zoned Date Time ‘2011-12-03T10:15:30+01:00[Europe/Paris]’
ISO_DATE_TIME Date and time with ZoneId ‘2011-12-03T10:15:30+01:00[Europe/Paris]’
ISO_ORDINAL_DATE Year and day of year ‘2012-337’
ISO_WEEK_DATE Year and Week ‘2012-W48-6’
ISO_INSTANT Date and Time of an Instant ‘2011-12-03T10:15:30Z’
RFC_1123_DATE_TIME RFC 1123 / RFC 822 ‘Tue, 3 Jun 2008 11:05:30 GMT’
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.time.*;
import java.time.format.DateTimeFormatter;


public class ZonedDateTimeFormatandParse {


    public static void main(String[] args) {

        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        System.out.println(zonedDateTime);

        String isoZonedDateTime = zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME);
        System.out.println(isoZonedDateTime);

        String isolocalDateTime = zonedDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        System.out.println(isolocalDateTime);

        String isolocalDate = zonedDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE);
        System.out.println(isolocalDate);

        String isoInstant = zonedDateTime.format(DateTimeFormatter.ISO_INSTANT);
        System.out.println(isoInstant);

        String isoWeekDate = zonedDateTime.format(DateTimeFormatter.ISO_WEEK_DATE);
        System.out.println(isoWeekDate);

    }
}

Output is:

1
2
3
4
5
6
2023-12-03T13:06:19.265173500+05:30[Asia/Calcutta]
2023-12-03T13:06:19.2651735+05:30[Asia/Calcutta]
2023-12-03T13:06:19.2651735
2023-12-03
2023-12-03T07:36:19.265173500Z
2023-W48-7+05:30

Formatting ZonedDateTime object using custom DateTimeFormatter

You can format ZonedDateTime using your own patterns as shown below.

Here you need to create DateTimeFormatter using DateTimeFormatter.ofPattern() which takes symbols as shown in below figure.

Ex: Pattern ‘dd/MM/yyyy - HH:mm:ss.N z’ will give string output as ‘03/12/2023 - 14:07:22.50842992734600 IST’.

You can use different combinations based on your requirement from the below table.

This tables shows how to create your patterns based on symbols.

Symbol Meaning Presentation Example
G era text AD; Anno Domini; A
u year year 2004; 04
y year-of-era year 2004; 04
D day-of-year number 189
M/L month-of-year number/text 7; 07; Jul; July; J
d day-of-month number 10
g modified-julian-day number 2451334
Q/q quarter-of-year number/text 3; 03; Q3; 3rd quarter
Y week-based-year year 1996; 96
w week-of-week-based-year number 27
W week-of-month number 4
E day-of-week text Tue; Tuesday; T
e/c localized day-of-week number/text ‘20111203’
F day-of-week-in-month number 3
a am-pm-of-day text PM
B period-of-day text in the morning
h clock-hour-of-am-pm (1-12) number 12
K hour-of-am-pm (0-11) number 0
k clock-hour-of-day (1-24) number 24
H hour-of-day (0-23) number 0
m minute-of-hour number 30
s second-of-minute number 55
S fraction-of-second fraction 978
A milli-of-day number 1234
n nano-of-second number 987654321
N nano-of-day number 1234000000
V time-zone ID zone-id America/Los_Angeles; Z; -08:30
v generic time-zone name zone-name Pacific Time; PT
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

Below code highlights how to create formatter from DateTimeFormatter.ofPattern() method by using various string patterns.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.time.*;
import java.time.format.DateTimeFormatter;


public class ZonedDateTimeFormatandParse {


    public static void main(String[] args) {

        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        System.out.println(zonedDateTime);

        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("dd/MM/yyyy - HH:mm:ss.N z");
        String formattedString1 = zonedDateTime.format(formatter1);
        System.out.println(formattedString1);

        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/dd/yyyy - kk:mm:ss X");
        String formattedString2 = zonedDateTime.format(formatter2);
        System.out.println(formattedString2);

        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("dd/MM/yyyy - HH:mm:ss x");
        String formattedString3 = zonedDateTime.format(formatter3);
        System.out.println(formattedString3);

        DateTimeFormatter formatter4 = DateTimeFormatter.ofPattern("dd/MM/yyyy - HH:mm:ss VV");
        String formattedString4 = zonedDateTime.format(formatter4);
        System.out.println(formattedString4);

        DateTimeFormatter formatter5 = DateTimeFormatter.ofPattern("dd/MM/yyyy ww - HH:mm:ss v");
        String formattedString5 = zonedDateTime.format(formatter5);
        System.out.println(formattedString5);

        DateTimeFormatter formatter6 = DateTimeFormatter.ofPattern("dd/MM/yyyy KKa - HH:mm:ss X");
        String formattedString6 = zonedDateTime.format(formatter6);
        System.out.println(formattedString6);


    }
}
1
2
3
4
5
6
7
2023-12-03T14:07:22.992734600+05:30[Asia/Calcutta]
03/12/2023 - 14:07:22.50842992734600 IST
12/03/2023 - 14:07:22 +0530
03/12/2023 - 14:07:22 +0530
03/12/2023 - 14:07:22 Asia/Calcutta
03/12/2023 49 - 14:07:22 IT
03/12/2023 02pm - 14:07:22 +0530

Conclusion

Thus from this tutorial we have learnt how to format ZonedDateTime in Java.