How to convert Java Date into Specific TimeZone format

Filed Under: Java

In the last example, we learned how to convert Date to String in Java. In this example, I am converting a Java Date object from one timezone to another.

We will use SimpleDateFormat class to format the Date in a specific format and we will set its timezone to print the date in a specific timezone.

package com.journaldev.util;

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

public class DateFormatter {

	 * Utility function to convert java Date to TimeZone format
	 * @param date
	 * @param format
	 * @param timeZone
	 * @return
	public static String formatDateToString(Date date, String format,
			String timeZone) {
		// null check
		if (date == null) return null;
		// create SimpleDateFormat object with input format
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		// default system timezone if passed null or empty
		if (timeZone == null || "".equalsIgnoreCase(timeZone.trim())) {
			timeZone = Calendar.getInstance().getTimeZone().getID();
		// set timezone to SimpleDateFormat
		// return Date in required format with timezone as String
		return sdf.format(date);

	public static void main(String[] args) {
		//Test formatDateToString method
		Date date = new Date();
		System.out.println("Default Date:"+date.toString());
		System.out.println("System Date: "+formatDateToString(date, "dd MMM yyyy hh:mm:ss a", null));
		System.out.println("System Date in PST: "+formatDateToString(date, "dd MMM yyyy hh:mm:ss a", "PST"));
		System.out.println("System Date in IST: "+formatDateToString(date, "dd MMM yyyy hh:mm:ss a", "IST"));
		System.out.println("System Date in GMT: "+formatDateToString(date, "dd MMM yyyy hh:mm:ss a", "GMT"));


Here is the output of the program:

Default Date:Wed Nov 14 21:37:01 PST 2012
System Date: 14 Nov 2012 09:37:01 PM
System Date in PST: 14 Nov 2012 09:37:01 PM
System Date in IST: 15 Nov 2012 11:07:01 AM
System Date in GMT: 15 Nov 2012 05:37:01 AM

From the output, it’s clear that my system TimeZone is PST and then it’s converting same Date object to different timezones like IST and GMT and printing it. Using my last tutorial you can again convert the returned string to a Date object.

Update: Java 8 has added new Date Time API, you should check it out at Java 8 Date.


  1. Teja says:

    How to convert the time in 24-hours format? Instead of showing in AM/PM.

    1. Uwe says:

      Using HH instead of hh, the hour is displayed as 00 to 23. The a character in the format results in AM or PM, so it is not necessary if you use HH for hours. Pankaj, thank you for the code.

  2. Kenneth says:

    What if I want to convert that output string back to another Date object? Do I simply use the parse() method?

  3. Mridu says:

    Thanks a lot !

  4. Ravi says:

    Really Helpful!!!!!!

  5. Jalpa Bhaliya says:

    I tried above code in eclipse android to convert date according to timezone but I am getting same date and time for PST, IST, GMT. can you please say why I am not getting correct converted date and time.

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors