How to Convert Java Date into Specific TimeZone Format

Introduction

In the last example, we learned how to convert a object to a String in Java. Converting dates is essential for formatting and displaying data in readable and localized formats. In this example, we will convert a Java Date object from one timezone to another. This process is useful when working with global users across different timezones. We will use the SimpleDateFormat class to define a specific date format. Then, we will set its timezone to match the desired location. This approach ensures accurate and user-friendly date representations. It is especially helpful for applications requiring time-sensitive information. Let’s explore how to do this effectively.

Convert Java Date: Code Example

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"));
    }

}

Output to Convert Java Date

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 to Convert Java Date, it’s clear that my system’s default TimeZone is set to PST. The program uses this default timezone to convert the same Date object into other specific timezones. These include IST (Indian Standard Time) and GMT (Greenwich Mean Time), which are commonly used. Each conversion shows how SimpleDateFormat and timezone settings work to transform the Date object. This allows the same Date to display correctly across various regions and locations. The program ensures accurate conversions by printing results for each timezone. This approach is essential for global applications needing localized date and time formats. It demonstrates practical usage and verifies functionality.

Additional Information

Using my last tutorial as a reference, you can easily convert the returned string back into a Date object. This flexibility allows developers to seamlessly switch between string and Date formats based on the application’s requirements. Update: With the release of Java 8, a powerful new Date Time API has been introduced, offering enhanced functionality and greater precision for handling date and time operations. The new API simplifies complex date manipulations, improves readability, and reduces potential errors in code. You should definitely explore the Java 8 Date Time API to take advantage of these improvements and streamline your date-related programming tasks.