Home >Java >javaTutorial >How to Accurately Convert Date and Time to a Different Time Zone in Java?

How to Accurately Convert Date and Time to a Different Time Zone in Java?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-29 09:44:30508browse

How to Accurately Convert Date and Time to a Different Time Zone in Java?

Convert Date and Time to a Different Time Zone in Java

When attempting to convert the current date and time to a different time zone, a common issue arises when the output time is inconsistent with the expected value. To correct this, consider the following solution:

<code class="java">import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateAndTimeZoneConversion {

    public static void main(String[] args) {
        try {
            // Get the current date and time
            Calendar currentDate = Calendar.getInstance();
            String strDate = null;
            DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
            strDate = formatter.format(currentDate.getTime());

            // Convert from current time zone to IST
            TimeZone fromTimeZone = TimeZone.getDefault();
            TimeZone istTimeZone = TimeZone.getTimeZone("IST");

            currentDate.setTimeZone(fromTimeZone);
            currentDate.add(Calendar.MILLISECOND, fromTimeZone.getRawOffset() * -1);
            if (fromTimeZone.inDaylightTime(currentDate.getTime())) {
                currentDate.add(Calendar.MILLISECOND, currentDate.getTimeZone().getDSTSavings() * -1);
            }

            // Convert to Central Standard Time (CST)
            TimeZone toTimeZone = TimeZone.getTimeZone("CST");
            currentDate.add(Calendar.MILLISECOND, toTimeZone.getRawOffset());
            if (toTimeZone.inDaylightTime(currentDate.getTime())) {
                currentDate.add(Calendar.MILLISECOND, toTimeZone.getDSTSavings());
            }

            // Print the converted time
            System.out.println("The current time in India is: " + istTimeZone.getDisplayName() + " :: " + formatter.format(currentDate.getTime()));
            System.out.println("The current time in Central Standard Time is: " + toTimeZone.getDisplayName() + " :: " + formatter.format(currentDate.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}</code>

This code addresses the time gap by properly accounting for offsets and daylight saving time for both the source and destination time zones, ensuring accurate time conversion.

The above is the detailed content of How to Accurately Convert Date and Time to a Different Time Zone in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn