Java在时区之间转换数据/时间不准确

时间:2016-04-18 06:02:41

标签: java timezone

    TimeZone.setDefault(TimeZone.getTimeZone("Europe/Moscow"));
    System.out.println("Default Timezone: " + TimeZone.getDefault());
    String date = "08/04/2016 00:00:00";
    SimpleDateFormat simpleDateFormatMoscow = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    Date moscowDt = simpleDateFormatMoscow.parse(date);
    System.out.println("Moscow Date: " + simpleDateFormatMoscow.format(moscowDt));
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Bangkok"));
    System.out.println("Bangkok Date: " + simpleDateFormat.format(moscowDt));
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(moscowDt);
    calendar.setTimeZone(TimeZone.getTimeZone("Asia/Bangkok"));
    System.out.println("Bangkok Date: " + simpleDateFormat.format(calendar.getTime()));
    System.out.println("Test Timezone");
    System.out.println(TimeZone.getTimeZone("America/New_York"));
    System.out.println(TimeZone.getTimeZone("Europe/Moscow"));
    System.out.println(TimeZone.getTimeZone("Asia/Bangkok"));

我尝试使用此代码段的代码来转换莫斯科和曼谷之间的日期/时间。结果如下:

默认时区:

  

sun.util.calendar.ZoneInfo [ID ="欧/莫斯科",偏移= 14400000,dstSavings = 0,useDaylight =假,转换= 78,lastRule =空]

     

莫斯科日期:08/04/2016 00:00:00

     

// util date / time

     

曼谷日期:08/04/2016 03:00:00

     

// joda time

     

曼谷日期:08/04/2016 03:00:00

但是,当我使用https://singztechmusings.wordpress.com/2011/06/23/java-timezone-correctionconversion-with-daylight-savings-time-settings/或谷歌转换日期/时间时,

  

莫斯科日期:08/04/2016 00:00:00

     

曼谷日期:08/04/2016 04:00:00

有谁能告诉我使用java转换数据/时间的正确方法? 谁能告诉我我做错了什么以及为什么结果不准确?

3 个答案:

答案 0 :(得分:2)

Java正在使用自己的时区数据,该数据与主机操作系统无关。如果你没有使用最新版本的Java导致俄罗斯(欧洲/莫斯科)有switched from daylight saving time to permanent standard time two years ago

,这可能是不准确的

答案 1 :(得分:2)

你的Java有错误的时区偏移:"偏移= 14400000"是4小时,但去年莫斯科是UTC + 3。

使用tzupdater升级你的java。

答案 2 :(得分:0)

这是首先使用当地时区进行此操作的方法。

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 Bangkok time zone. Then we set
    //the time of the calendar with the value of the local time

    Calendar BangkokTime = new GregorianCalendar(TimeZone.getTimeZone("Asia/Bangkok"));
    BangkokTime.setTimeInMillis(localTime.getTimeInMillis());
    hour = BangkokTime.get(Calendar.HOUR);
    minute = BangkokTime.get(Calendar.MINUTE);
    second = BangkokTime.get(Calendar.SECOND);


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


    //Then do the same for the Moscow time zone
    Calendar MoscowTime = new GregorianCalendar(TimeZone.getTimeZone("Europe/Moscow"));
    MoscowTime.setTimeInMillis(localTime.getTimeInMillis());
    hour = MoscowTime.get(Calendar.HOUR);
    minute = MoscowTime.get(Calendar.MINUTE);
    second = MoscowTime.get(Calendar.SECOND);


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