如何在 Java 中转换 UTC 和本地时区
我对 Java 中的时区很好奇.我想从设备获取 UTC 时间(以毫秒为单位)并发送到服务器.服务器在向用户显示时间时会将其转换为本地时区.我系统中的时区是澳大利亚/悉尼(UTC + 11:00),我在测试时区时得到以下结果:
I am curious about timezone in Java. I want to get UTC time in milliseconds from a device and send to server. Server will convert it to local timezone when it displays time to users. Timezone in my system is Australia/Sydney( UTC + 11:00), and I have got the result below when I tested timezone:
int year = 2014;
int month = 0;
int date = 14;
int hourOfDay = 11;
int minute = 12;
int second = 0;
Calendar c1 = Calendar.getInstance();
c1.set(year, month, date, hourOfDay, minute, second);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss z");
System.out.println(sdf.format(c1.getTime()));
Calendar c2 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
c2.set(year, month, date, hourOfDay, minute, second);
System.out.println(sdf.format(c2.getTime()));
输出:
14/01/2014 11:12:00 EST
14/01/2014 22:12:00 EST
我认为我可以为 c2 设置 13/01/2014 00:12:00,因为 UTC 时间比我的晚 11 小时.日历不是按我预期的方式工作吗?
I thought I could have 13/01/2014 00:12:00 for c2 because UTC time is 11 hours later than mine. Does not Calendar work the way I expect?
您的帮助将不胜感激.
添加了 z 以显示时区.这让我更加困惑,因为 Mac 说它的时区是(AEDT)澳大利亚东部夏令时间,但 Java 是 EST.无论如何,结果仍然不同,因为 EST 是 UTC-5 小时.
Added z to display timezone. This makes me more confused because Mac says its timezone is (AEDT) Australian Eastern Daylight Time but Java is EST. Anyway still result is different because EST is UTC-5 hours.
推荐答案
您可能打算在格式化程序上设置时区,而不是日历(或者除了日历,还不是 100% 清楚您要完成什么)!用于创建人类表示的时区来自 SimpleDateFormat.当您通过调用 getTime()
将其转换回 java.util.Date 时,日历中的所有时区"信息都会丢失.
You probably meant to set the timezone on your formatter, not the Calendar (or in addition the the Calendar, it is not 100% clear what you mean to accomplish)! The timezone used to create the human representation comes from the SimpleDateFormat. All "timezone" information is lost from the Calendar when you convert it back into a java.util.Date by calling getTime()
.
代码:
Calendar c2 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
c2.set(year, month, date, hourOfDay, minute, second);
System.out.println(sdf.format(c2.getTime()));
正在打印 14/01/2014 10:12:00
,因为 Syndey(格式化程序的时区)中显示的 11AM UTC 是晚上 10 点!(使用 HH 格式表示 24 小时时间)
is printing 14/01/2014 10:12:00
because 11AM UTC displayed in Syndey (the timezone of your formatter) is 10PM! (use HH in the format for 24 hour time)
这会打印出你想要做的事情:
This would print what it seems like you meant to do:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss z");
System.out.println(sdf.format(c1.getTime()));
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(sdf.format(c1.getTime()));
UTC 毫秒"的概念毫无意义.毫秒数只是历史上的一个固定点,它没有与之关联的时区.我们向其添加时区以将其转换为人类可读的表示形式.
The concept of 'UTC milliseconds' is meaningless. A quantity of milliseconds is just a fixed point in history, it has no timezone associated with it. We add a timezone to it to convert it into human-readable representations.
是的,对于(美国)东部时间和(澳大利亚)东部时间都使用EST"的歧义一直是 Java 中的一个陷阱.
edit: Yes, the ambiguity of using 'EST' for both (US) Eastern Time and (Australian) Eastern Time has been a pitfall in Java since forever.
相关文章