如何在Java中转换UTC和本地时区 [英] How to convert UTC and local timezone in Java

查看:175
本文介绍了如何在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

因为UTC时间比我的晚11个小时,所以我认为我可以在13/01/2014 00:12:00使用c2。日历不按照我的预期工作吗?

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中显示的上午11点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的模糊性和(澳大利亚)东部时间一直是爪哇的一个陷阱。

edit: Yes, the ambiguity of using 'EST' for both (US) Eastern Time and (Australian) Eastern Time has been a pitfall in Java since forever.

这篇关于如何在Java中转换UTC和本地时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆