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

    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日期/时间

曼谷日期: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有错误的时区偏移量:“offset = 14400000”是4小时,但是去年的莫斯科是UTC + 3。

用tzupdater升级你的java。


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


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

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:%02dn", 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:%02dn", 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:%02dn", hour, minute, second);
}
链接地址: http://www.djcxy.com/p/397.html

上一篇: Java convert data/time between timezone is inaccurate

下一篇: Java and JavaScript timestamps are not the same