java 如果定义了 TimeZone,则 Calendar.getTime() 不返回 UTC 日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17233905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calendar.getTime() not returning UTC date if TimeZone is defined
提问by Parkash Kumar
I have done this for my Calendar instance to return Date in UTC timezone:
我已经为我的 Calendar 实例执行了此操作,以在 UTC 时区返回日期:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:SS Z");
TimeZone tz = TimeZoneUtil.getTimeZone(StringPool.UTC);
formatter.setTimeZone(tz);
Date dtStart = null;
Date dtEnd = null;
try{
dtStart = formatter.parse(formatter.format(startDate.getTime()));
dtEnd = formatter.parse(formatter.format(endDate.getTime()));
}catch (Exception e) {
e.getStackTrace();
}
It works fine till I format calendar timestamp to return a string date with required timezone but when I parse that string date to Date date, it again picks up local timezone? I need to store Date object in UTC timezone.
它工作正常,直到我格式化日历时间戳以返回具有所需时区的字符串日期,但是当我将该字符串日期解析为日期日期时,它再次选择本地时区?我需要将 Date 对象存储在 UTC 时区。
Any help will be highly appreciated!
任何帮助将不胜感激!
回答by Vahe
You can use this:
你可以使用这个:
Date localTime = new Date();
//creating DateFormat for converting time from local timezone to GMT
DateFormat converter = new SimpleDateFormat("dd/MM/yyyy:HH:mm:ss");
//getting GMT timezone, you can get any timezone e.g. UTC
converter.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("local time : " + localTime);;
System.out.println("time in GMT : " + converter.format(localTime));
It will give:
local time: Fri Jun 21 11:55:00 UTC 2013
time in GMT : 21/06/2013:11:55:00
它将给出:
当地时间:6 月 21 日星期五 11:55:00 UTC 2013
年格林威治标准时间:21/06/2013:11:55:00
I hope it will help.
我希望它会有所帮助。
Cheers.
干杯。
回答by Abubakkar
Date object in java will always store the values in the host machine (your system) time zone information.
Java 中的日期对象将始终将值存储在主机(您的系统)时区信息中。
This is from javadoc :
这是来自 javadoc :
Although the Date class is intended to reflect coordinated universal time (UTC), it may not do so exactly, depending on the host environment of the Java Virtual Machine.
尽管 Date 类旨在反映协调世界时 (UTC),但它可能不会完全如此,具体取决于 Java 虚拟机的主机环境。
You should trying using Joda Time which is much advanced.
您应该尝试使用非常先进的Joda Time。
回答by kisna
Instead of setting TimeZone in multiple places, it is a good idea to set timezone using -Duser.timezone=GMT or PST.
与其在多个地方设置 TimeZone,不如使用 -Duser.timezone=GMT 或 PST 设置时区。
And, you can easily test how Java deals with timezone and getTime() ignores timezone with an actual example:
而且,您可以通过一个实际示例轻松测试 Java 如何处理时区和 getTime() 忽略时区:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); // print with timezone
TimeZone timeZone = TimeZone.getTimeZone(ZoneId.of("GMT"));
TimeZone.setDefault(timeZone); // set system timezone as GMT
sdf.setTimeZone(timeZone); // formatter also has a timezone
Date date = new Date();
System.out.println(date); // system says GMT date
System.out.println(date.getTime()); // only prints time in milliseconds after January 1, 1970 00:00:00 GMT
System.out.println(sdf.format(date));
timeZone = TimeZone.getTimeZone(ZoneId.of("America/Los_Angeles"));
TimeZone.setDefault(timeZone); // set system timezone as GMT
sdf.setTimeZone(timeZone); // formatter also has a timezone
date = new Date();
System.out.println(date);
System.out.println(date.getTime()); // prints the same value as above, "not including timezone offset"
System.out.println(sdf.format(date));
// GMT and PDT times are same as getTime() only returns time in ms since UTC for the day ignoring timezone which is mostly used for formatting
Wed Mar 14 22:43:43 GMT 2018
1521067423108
2018-03-14T22:43:43+0000
Wed Mar 14 15:43:43 PDT 2018
1521067423125 // not includes timezone in getTime()
2018-03-14T15:43:43-0700 // formatting looks fine
回答by dharam
The good explanation of why Date object taking Current time zone value ,
很好地解释了为什么 Date 对象采用当前时区值,
please refer this SO answer
请参考这个SO 答案
EDIT.
编辑。
here I am gonna add some important part of that answers.
在这里,我将添加该答案的一些重要部分。
java.util.Date is has no specific time zone, although its value is most commonly thought of in relation to UTC. What makes you think it's in local time?
java.util.Date 没有特定的时区,尽管它的值最常被认为与 UTC 相关。是什么让你觉得现在是当地时间?
To be precise: the value within a java.util.Date is the number of milliseconds since the Unix epoch, which occurred at midnight January 1st 1970, UTC. The same epoch could also be described in other time zones, but the traditional description is in terms of UTC. As it's a number of milliseconds since a fixed epoch, the value within java.util.Date is the same around the world at any particular instant, regardless of local time zone.
准确地说:java.util.Date 中的值是自 Unix 纪元以来的毫秒数,该纪元发生在 UTC 时间 1970 年 1 月 1 日午夜。同一个纪元也可以用其他时区来描述,但传统的描述是用 UTC 来描述的。由于它是固定纪元以来的毫秒数,因此无论本地时区如何,java.util.Date 中的值在世界各地的任何特定时刻都是相同的。