Java : Date Format and TimeZone Conversion

In below example convert date to different date format and time zone.

Example

package com.date;

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

public class Date2TimeZoneUtility {
public static String LEGACY_FORMAT = "EEE MMM dd hh:mm:ss zzz yyyy";
public static String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS zzz";
public static String SHORT_DATE = "yyyy-MM-dd";
private static final TimeZone utc = TimeZone.getTimeZone("UTC");
private static final SimpleDateFormat legacyFormatter = new SimpleDateFormat(LEGACY_FORMAT);
private static final SimpleDateFormat isoFormatter = new SimpleDateFormat(ISO_FORMAT);
private static final SimpleDateFormat shortFormatter = new SimpleDateFormat(SHORT_DATE);
static {
		legacyFormatter.setTimeZone(utc);
		isoFormatter.setTimeZone(utc);
	}

public static void main(String[] args) {
Date2TimeZoneUtility dateUtility = new Date2TimeZoneUtility();
Date date = new Date();
System.out.println("Current Date Default:" + date);
System.out.println("--->Change Date Format");
// Legacy Date Format
System.out.println("Current Date Legacy Format:" + dateUtility.toString(date, legacyFormatter));
// ISO Date Format
System.out.println("Current Date ISO Format:" + dateUtility.toString(date, isoFormatter));
// Short Date Format
System.out.println("Current Date Short Format:" + dateUtility.toString(date, shortFormatter));
System.out.println("--->Change Date Format with Time Zone UTC");
// Legacy Date Format UTC TimeZone
System.out.println("Current Date Legacy Format:" + dateUtility.toLegacyFormat(date));
// ISO Date Format UTC TimeZone
System.out.println("Current Date ISO Format:" + dateUtility.toISOFormat(date));
// Short Date Format UTC TimeZone
System.out.println("Current Date Short Format:" + dateUtility.toShortFormat(date));
System.out.println("--->Change Date Format with different Time Zone");
// Legacy Date Format with time Zone EST
System.out.println("Current Date Legacy Format with TimeZone EST:"
				+ dateUtility.toStringWithTimeZone(date, legacyFormatter, "EST"));
// ISO Date Format with time zone IST
System.out.println( "Current Date ISO Format with TimeZone IST:" + dateUtility.toStringWithTimeZone(date, isoFormatter, "IST"));
// Short Date Format Short Date with PST
System.out.println("Current Date Short Format with TimeZone PST:"
				+ dateUtility.toStringWithTimeZone(date, shortFormatter, "PST"));
}

public String toString(final Date date, SimpleDateFormat dateformat) {
		return dateformat.format(date);
}

public String toStringWithTimeZone(final Date date, SimpleDateFormat dateformat, final String timezone) {
		final TimeZone tz = TimeZone.getTimeZone(timezone);
		dateformat.setTimeZone(tz);
		return dateformat.format(date);
}

public String toLegacyFormat(final Date date) {
		return legacyFormatter.format(date);
}

public String toISOFormat(final Date date) {
		return isoFormatter.format(date);
}

public String toShortFormat(final Date date) {
		return shortFormatter.format(date);
}

}

Output


Current Date Default:Sat Jul 28 12:58:38 PDT 2018
--->Change Date Format
Current Date Legacy Format:Sat Jul 28 07:58:38 UTC 2018
Current Date ISO Format:2018-07-28T19:58:38.932 UTC
Current Date Short Format:2018-07-28
--->Change Date Format with Time Zone UTC
Current Date Legacy Format:Sat Jul 28 07:58:38 UTC 2018
Current Date ISO Format:2018-07-28T19:58:38.932 UTC
Current Date Short Format:2018-07-28
--->Change Date Format with different Time Zone
Current Date Legacy Format with TimeZone EST:Sat Jul 28 02:58:38 EST 2018
Current Date ISO Format with TimeZone IST:2018-07-29T01:28:38.932 IST
Current Date Short Format with TimeZone PST:2018-07-28