How to Format TimeZone Using Java SimpleDateFormat


In this example we will show how to format TimeZone using java SimpleDateFormat class. Here TimeZone can be formatted in either z, zzzz or Z formats.

Source Code

package com.beginner.examples;

import java.text.SimpleDateFormat;
import java.util.Date;

public class FormattingTimeZone {

	public static void main(String[] args) {
		
		//create Date.
		Date date = new Date();
		
		/*
		 * To format TimeZone in z (General time zone) formate use SimpleDateFormat("z") Constructor.
		 */
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("z");
		System.out.println("TimeZone in z format : " + simpleDateFormat1.format(date));
		
		/*
		 * formatting TimeZone in zzzz format Eastern Standard Time use SimpleDateFormat("zzzz") Constructor.
		 */
		SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("zzzz");
		System.out.println("TimeZone in zzzz format : " + simpleDateFormat2.format(date));
		
		/*
		 * formatting TimeZone in Z (RFC 822) use SimpleDateFormat("Z") Constructor.
		 */
		SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("Z");
		System.out.println("TimeZone in Z format : " + simpleDateFormat3.format(date));
		
		/*
		 * formatting TimeZone in X (ISO 8601) use SimpleDateFormat("X") Constructor.
		 */
		SimpleDateFormat simpleDateFormat4 = new SimpleDateFormat("X");
		System.out.println("TimeZone in X format : " + simpleDateFormat4.format(date));
		
	}

}

Output:

TimeZone in z format : CST
TimeZone in zzzz format : Eastern Standard Time
TimeZone in zzzz format : +0800
TimeZone in zzzz format : +08

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments