Java 8 – How to convert ZonedDateTime in different Format Style ?

In this article, we will learn how to format ZonedDateTime in different Format Style provided in Java 1.8 version

1. Format ZonedDateTime in different Format Style :

  • FormatStyle class provides 4 different enum constants for formatting ZonedDateTime, those are
    1. FormatStyle.SHORT
    2. FormatStyle.MEDIUM
    3. FormatStyle.LONG
    4. FormatStyle.FULL
  • In below illustration, we are using above provided in-built formats to format ZonedDateTime as listed below,
    1. DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
    2. DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
    3. DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
    4. DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
  • Note: for creating above formats we need DateTimeFormatter

2. ZonedDateTime examples in different Format Style :

2.1 ZonedDateTime to FormatStyle.SHORT format :

  • This format style converts ZonedDateTime in default (yyyy-MM-ddTHH:mm:ss.nnn+HH:mm[Region/City]) format to (dd/MM/yy, hh:mm a) format ignoring second & nanosecond fields

FormatZonedDateTimeToShortStyle.java

package in.bench.resources.java8.zoneddatetime.examples;

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

public class FormatZonedDateTimeToShortStyle {

	public static void main(String[] args) {

		// 1. get Zoned Date/time
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		System.out.println("Zoned Date/time is :- \n" + zonedDateTime);


		// 2. DateTimeFormatter
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter
				.ofLocalizedDateTime(FormatStyle.SHORT);


		// 3. ZonedDateTime to FormatStyle.SHORT format in String-form
		String str = zonedDateTime.format(dateTimeFormatter);
		System.out.print("\nZonedDateTime to FormatStyle.SHORT format :- \n"  + str);
	}
}

Output:

Zoned Date/time is :- 
2022-08-13T01:40:47.739222100+05:30[Asia/Calcutta]

ZonedDateTime to FormatStyle.SHORT format :- 
13/08/22, 1:40 am

2.2 ZonedDateTime to FormatStyle.MEDIUM format :

  • This format style converts ZonedDateTime in default (yyyy-MM-ddTHH:mm:ss.nnn+HH:mm[Region/City]) format to (dd-MMM-yyyy, hh:mm:ss a) format ignoring nanosecond field

FormatZonedDateTimeToMediumStyle.java

package in.bench.resources.java8.zoneddatetime.examples;

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

public class FormatZonedDateTimeToMediumStyle {

	public static void main(String[] args) {

		// 1. get Zoned Date/time
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		System.out.println("Zoned Date/time is :- \n" + zonedDateTime);


		// 2. DateTimeFormatter
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter
				.ofLocalizedDateTime(FormatStyle.MEDIUM);


		// 3. ZonedDateTime to FormatStyle.MEDIUM format in String-form
		String str = zonedDateTime.format(dateTimeFormatter);
		System.out.print("\nZonedDateTime to FormatStyle.MEDIUM format :- \n"  + str);
	}
}

Output:

Zoned Date/time is :- 
2022-08-13T01:41:09.109723600+05:30[Asia/Calcutta]

ZonedDateTime to FormatStyle.MEDIUM format :- 
13-Aug-2022, 1:41:09 am

2.3 ZonedDateTime to FormatStyle.LONG format :

  • This format style converts ZonedDateTime in default (yyyy-MM-ddTHH:mm:ss.nnn+HH:mm[Region/City]) format to (dd MMMM yyyy at hh:mm:ss a z) format ignoring nanosecond field

FormatZonedDateTimeToLongStyle.java

package in.bench.resources.java8.zoneddatetime.examples;

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

public class FormatZonedDateTimeToLongStyle {

	public static void main(String[] args) {

		// 1. get Zoned Date/time
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		System.out.println("Zoned Date/time is :- \n" + zonedDateTime);


		// 2. DateTimeFormatter
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter
				.ofLocalizedDateTime(FormatStyle.LONG);


		// 3. ZonedDateTime to FormatStyle.LONG format in String-form
		String str = zonedDateTime.format(dateTimeFormatter);
		System.out.print("\nZonedDateTime to FormatStyle.LONG format :- \n"  + str);
	}
}

Output:

Zoned Date/time is :- 
2022-08-13T01:42:01.991101800+05:30[Asia/Calcutta]

ZonedDateTime to FormatStyle.LONG format :- 
13 August 2022 at 1:42:01 am IST

2.4 ZonedDateTime to FormatStyle.FULL format :

  • This format style converts ZonedDateTime in default (yyyy-MM-ddTHH:mm:ss.nnn+HH:mm[Region/City]) format to (EEEE, dd MMMM, yyyy at hh:mm:ss a z) format ignoring nanosecond field

FormatZonedDateTimeToFullStyle.java

package in.bench.resources.java8.zoneddatetime.examples;

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

public class FormatZonedDateTimeToFullStyle {

	public static void main(String[] args) {

		// 1. get Zoned Date/time
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		System.out.println("Zoned Date/time is :- \n" + zonedDateTime);


		// 2. DateTimeFormatter
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter
				.ofLocalizedDateTime(FormatStyle.FULL);


		// 3. ZonedDateTime to FormatStyle.FULL format in String-form
		String str = zonedDateTime.format(dateTimeFormatter);
		System.out.print("\nZonedDateTime to FormatStyle.FULL format :- \n"  + str);
	}
}

Output:

Zoned Date/time is :- 
2022-08-13T01:42:57.270655200+05:30[Asia/Calcutta]

ZonedDateTime to FormatStyle.FULL format :- 
Saturday, 13 August, 2022 at 1:42:57 am India Standard Time

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert ZonedDateTime to LocalDateTime ?
Java 8 – How to convert ZonedDateTime in different formats ?