【发布时间】: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?