Tutorial

How to convert Java Date into Specific TimeZone format

Published on August 3, 2022
Default avatar

By Pankaj

How to convert Java Date into Specific TimeZone format

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

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
		sdf.setTimeZone(TimeZone.getTimeZone(timeZone));
		// 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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
July 30, 2020

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

- Teja

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    September 25, 2019

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

    - Kenneth

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      August 13, 2019

      Thanks a lot !

      - Mridu

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        May 22, 2015

        Really Helpful!!!

        - Ravi

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          April 10, 2014

          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.

          - Jalpa Bhaliya

            Try DigitalOcean for free

            Click below to sign up and get $200 of credit to try our products over 60 days!

            Sign up

            Join the Tech Talk
            Success! Thank you! Please check your email for further details.

            Please complete your information!

            Get our biweekly newsletter

            Sign up for Infrastructure as a Newsletter.

            Hollie's Hub for Good

            Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

            Become a contributor

            Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

            Welcome to the developer cloud

            DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

            Learn more
            DigitalOcean Cloud Control Panel