【问题标题】:Java convert data/time between timezone is inaccurateJava 在时区之间转换数据/时间不准确
【发布时间】:2016-04-18 06:02:41
【问题描述】:
    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"));

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

默认时区:

sun.util.calendar.ZoneInfo[id="Europe/Moscow",offset=14400000,dstSavings=0,useDaylight=false,transitions=78,lastRule=null]

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

//使用日期/时间

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

//乔达时间

曼谷日期: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转换数据/时间的正确方法? 谁能告诉我我做错了什么以及为什么结果不准确?

【问题讨论】:

  • 您使用的是什么版本的 Java?

标签: java timezone


【解决方案1】:

您的 Java 有错误的时区偏移:“offset=14400000”是 4 小时,但莫斯科是 UTC+3 去年半。

使用 tzupdater 升级您的 java。

【讨论】:

  • 非常感谢米哈伊尔·库奇马。 tzupdater 之后,结果和方面一样。莫斯科日期:08/04/2016 00:00:00 曼谷日期:08/04/2016 04:00:00
  • 这很难说——它只取决于你的需要。至于我 - 我只做过一次,只是针对莫斯科时区。
  • Time in Russia近年来变化频繁。 Boundaries have been changing。夏令时(DST) has been changing,在 2011 年和 2014 年重新定义。
【解决方案2】:

Java 使用自己的时区数据,该数据独立于主机操作系统。如果您没有使用最新版本的 Java,则可能不准确,因为俄罗斯(欧洲/莫斯科)有 switched from daylight saving time to permanent standard time two years ago

【讨论】:

  • 非常感谢bedrin。确实我的java时区没有更新。
【解决方案3】:

这是首先使用当地时区的一种方法。

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

【讨论】:

  • 非常感谢您的回答凯尔隆里奇。完成tzupdater后,结果如预期。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-06
  • 1970-01-01
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
  • 2017-03-05
  • 1970-01-01
相关资源
最近更新 更多