Tutorial

How to convert Java Date into Specific TimeZone format

Published on August 4, 2022
author

Pankaj

How to convert Java Date into Specific TimeZone format

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 our products

About the authors
Default avatar
Pankaj

author

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.

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!

            Become a contributor for community

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

            DigitalOcean Documentation

            Full documentation for every DigitalOcean product.

            Resources for startups and SMBs

            The Wave has everything you need to know about building a business, from raising funding to marketing your product.

            Get our newsletter

            Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

            New accounts only. By submitting your email you agree to our Privacy Policy

            The developer cloud

            Scale up as you grow — whether you're running one virtual machine or ten thousand.

            Get started for free

            Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

            *This promotional offer applies to new accounts only.