Timezone conversion

2018-12-31 14:58发布

I need to convert from one timezone to another timezone in my project.

I am able to convert from my current timezone to another but not from a different timezone to another.

For example I am in India, and I am able to convert from India to US using Date d=new Date(); and assigning it to a calendar object and setting the time zone.

However, I cannot do this from different timezone to another timezone. For example, I am in India, but I am having trouble converting timezones from the US to the UK.

11条回答
回忆,回不去的记忆
2楼-- · 2018-12-31 15:32

If you don't want to use Joda, here is a deterministic way using the built in libraries.

First off I recommend that you force your JVM to default to a timezone. This addresses the issues you might run into as you move your JVM from one machine to another that are set to different timezones but your source data is always a particular timezone. For example, lets say your data is always PDT/PST time zone, but you run on a box that is set to UTC timezone.

The following code snippet sets the default timezone in my JVM:

 //You can either pass the JVM a parameter that 
 //enforces a TZ: java -Duser.timezone=UTC or you can do it
 //programatically like this
 TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
 TimeZone.setDefault(tz);

Now lets say your source date is coming in as PDT/PST but you need to convert it to UTC. These are the steps:

    DateFormat dateFormatUtc = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    dateFormatUtc.setTimeZone(TimeZone.getTimeZone("UTC"));

    String dateStrInPDT = "2016-05-19 10:00:00";
    Date dateInPDT = dateFormat.parse(dateStrInPDT);


    String dateInUtc = dateFormatUtc.format(dateInPDT);

    System.out.println("Date In UTC is " + dateInUtc);

The output would be:

Date In UTC is 2016-05-19 17:00:00
查看更多
妖精总统
3楼-- · 2018-12-31 15:35

Some examples

Convert time between timezone

Converting Times Between Time Zones

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class TimeZoneExample {
    public static void main(String[] args) {
        // Create a calendar object and set it time based on the local
        // time zone
        Calendar localTime = Calendar.getInstance();
        localTime.set(Calendar.HOUR, 17);
        localTime.set(Calendar.MINUTE, 15);
        localTime.set(Calendar.SECOND, 20);

        int hour = localTime.get(Calendar.HOUR);
        int minute = localTime.get(Calendar.MINUTE);
        int second = localTime.get(Calendar.SECOND);


        // Print the local time
        System.out.printf("Local time  : %02d:%02d:%02d\n", hour, minute, second);


        // Create a calendar object for representing a Germany time zone. Then we
        // wet the time of the calendar with the value of the local time

        Calendar germanyTime = new GregorianCalendar(TimeZone.getTimeZone("Europe/Berlin"));
        germanyTime.setTimeInMillis(localTime.getTimeInMillis());
        hour = germanyTime.get(Calendar.HOUR);
        minute = germanyTime.get(Calendar.MINUTE);
        second = germanyTime.get(Calendar.SECOND);


        // Print the local time in Germany time zone
        System.out.printf("Germany time: %02d:%02d:%02d\n", hour, minute, second);
    }
}
查看更多
千与千寻千般痛.
4楼-- · 2018-12-31 15:35
public static void convertTimeZone(Date date, TimeZone fromTimeZone, TimeZone toTimeZone) {
    long milliseconds = date.getTime();
    milliseconds += (fromTimeZone.getRawOffset() * -1);
    if (fromTimeZone.inDaylightTime(date)) {
        milliseconds += (fromTimeZone.getDSTSavings() * -1);
    }
    milliseconds += toTimeZone.getRawOffset();
    if (toTimeZone.inDaylightTime(date)) {
        milliseconds += toTimeZone.getDSTSavings();
    }
    date.setTime(milliseconds);
}
查看更多
若你有天会懂
5楼-- · 2018-12-31 15:40

You can use the following code snippet

String dateString = "14 Jul 2014 00:11:04 CEST";
date = formatter.parse(dateString);
System.out.println(formatter.format(date));

// Set the formatter to use a different timezone - Indochina Time
formatter.setTimeZone(TimeZone.getTimeZone("Asia/Bangkok"));
System.out.println("ICT time : "+formatter.format(date));
查看更多
冷夜・残月
6楼-- · 2018-12-31 15:41

The "default" time zone can be avoided entirely by just setting the time zone appropriately for the Calendar object. However, I would personally suggest that you use Joda Time as a far superior API for date and time operations in Java. Amongst other things, time zone conversion is very simple in Joda.

It's not clear what your current code looks like and why you're only able to convert via the default time zone, but in Joda Time you'd just specify the time zone explicitly when creating (say) a DateTime object, and then use withZone(DateTimeZone zone).

If you could tell us more about how you're getting input data, we could give a fuller example.

查看更多
泪湿衣
7楼-- · 2018-12-31 15:44
    Date date = new Date();
    String formatPattern = ....;
    SimpleDateFormat sdf = new SimpleDateFormat(formatPattern);

    TimeZone T1;
    TimeZone T2;

    // set the Calendar of sdf to timezone T1
    sdf.setTimeZone(T1);
    System.out.println(sdf.format(date));

    // set the Calendar of sdf to timezone T2
    sdf.setTimeZone(T2);
    System.out.println(sdf.format(date));

    // Use the 'calOfT2' instance-methods to get specific info
    // about the time-of-day for date 'date' in timezone T2.
    Calendar calOfT2 = sdf.getCalendar();
查看更多
登录 后发表回答