Java在时区之间转换日期/时间不准确。

3
    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="Europe/Moscow",offset=14400000,dstSavings=0,useDaylight=false,transitions=78,lastRule=null]

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

//util日期/时间

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

//joda时间

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

然而,当我使用https://singztechmusings.wordpress.com/2011/06/23/java-timezone-correctionconversion-with-daylight-savings-time-settings/进行日期/时间转换或者使用Google搜索时,结果是:

莫斯科日期: 2016年08月04日 00:00:00

曼谷日期: 2016年08月04日 04:00:00

请问有谁能告诉我使用Java正确转换日期/时间的方法?还有请问我哪里出错了,为什么结果不准确?


你使用的Java版本是什么? - bedrin
3个回答

2

Java使用自己的时区数据,与主机操作系统无关。如果您未使用最新版本的Java,则可能不准确,因为俄罗斯(欧洲/莫斯科)两年前已经从夏令时切换到永久标准时间


非常感谢您,bedrin。我的Java时区确实没有更新。 - Gibi

2

您的Java存在错误的时区偏移量:"offset=14400000"是4个小时,但莫斯科在过去的一年半时间内是UTC+3。

请使用tzupdater升级您的Java。


非常感谢Mikhail Kuchma。在使用tzupdater之后,结果如预期一样。莫斯科日期:2016年08月04日 00:00:00曼谷日期:2016年08月04日 04:00:00 - Gibi
1
很难说 - 这取决于你的需求。至于我 - 我只做过一次,仅针对莫斯科时区。 - Mikhail Kuchma
近年来,俄罗斯的时间经常发生变化。时区边界也在不断变化。夏令时在2011年和2014年重新定义后也发生了变化。 - Basil Bourque

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

非常感谢您的回答,Kyle Longrich。在我完成tzupdater之后,结果如预期。 - Gibi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接