在Java中获取GMT时间
在Java中,我想获取GMT的当前时间。
我尝试了这样的各种选项:
Date date = new Date();
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
date1 = calendar.getTime();
但日期总是以我当地的时区解释。
我做错了什么以及如何将 java 日期转换为 GMT?
In Java, I want to get the current time in GMT.
I tried various options like this:
Date date = new Date();
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
date1 = calendar.getTime();
But the date is always is interpreted in my local time zone.
What am I doing wrong and how can I convert a java Date to GMT?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
很有可能您在后端执行了正确的操作来获取日期,但没有任何迹象表明您没有采用 GMT 时间并根据计算机当前的区域设置对其进行格式化。
关键是使用你自己的DateFormat,而不是系统提供的DateFormat。这样你就可以将 DateFormat 的时区设置为你想要的,而不是设置为 Locale 的时区。
Odds are good you did the right stuff on the back end in getting the date, but there's nothing to indicate that you didn't take that GMT time and format it according to your machine's current locale.
The key is to use your own DateFormat, not the system provided one. That way you can set the DateFormat's timezone to what you wish, instead of it being set to the Locale's timezone.
我想知道为什么没有人这样做:
更新:
从 Java 8、9、10 及更高版本开始,Java 应该支持更好的替代方案。感谢您的评论@Humanity
I wonder why no one does this:
Update:
Since Java 8,9,10 and more, there should be better alternatives supported by Java. Thanks for your comment @humanity
根据我的经验,Java 中捆绑的 Calendar 和 Date 类可能会产生意想不到的效果。
如果您不介意升级到 Java 8,请考虑使用
ZonedDateTime
,如下所示:
From my experience, the bundled Calendar and Date classes in Java can yield undersired effect.
If you wouldn't mind upgrading to Java 8, then consider using
ZonedDateTime
like so:
tl;dr
java.time
Damilola 的回答正确地建议您使用 Java 8 中内置的 java.time 框架,并且之后。但该答案使用
ZonedDateTime
类,如果您只想要 UTC 而不是任何特定时区,则该类就太过分了。麻烦的旧日期时间类现在已被遗留,并被 java.time 类取代。
即时
即时
类表示 UTC 中时间轴上的时刻,分辨率为纳秒(最多九 (9) 位小数)。简单代码:
您可以将
Instant
视为可以添加时区 (ZoneID
) 的构建块获取ZonedDateTime
。关于 java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如
java.util.Date
,日历
,&SimpleDateFormat
。Joda-Time 项目,现位于 维护模式,建议迁移到 java.time。
要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310。
从哪里获取 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如
Interval
,YearWeek
,YearQuarter
,以及更多。tl;dr
java.time
The Answer by Damilola is correct in suggesting you use the java.time framework built into Java 8 and later. But that Answer uses the
ZonedDateTime
class which is overkill if you just want UTC rather than any particular time zone.The troublesome old date-time classes are now legacy, supplanted by the java.time classes.
Instant
The
Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).Simple code:
You can think of
Instant
as the building block to which you can add a time zone (ZoneID
) to get aZonedDateTime
.About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.您可以使用 Java 8
java.time
包获取Java 中的当前 GMT 时间:
将 java.util.Date 转换为 GMT
参考
You can achieve it using Java 8
java.time
packageGet current GMT time in Java:
Convert a java.util.Date to GMT
Ref
在尝试了很多方法之后,我发现,要获取 GMT 的毫秒时间,您需要创建两个单独的
SimpleDateFormat
对象,一个用于 GMT 格式,另一个用于解析。这是代码:
这工作正常。 :)
After trying a lot of methods, I found out, to get the time in millis at GMT you need to create two separate
SimpleDateFormat
objects, one for formatting in GMT and another one for parsing.Here is the code:
This works fine. :)
要获得格林威治标准时间(GMT)的毫秒时间,您所需要的只是
在进行以下工作时,它们效率很低。你也可以做
和
To get the time in millis at GMT all you need is
While the following work, they are inefficient. You can also do
and
你不能
首先,你在问不可能的事。老式的
Date
对象没有,因为不能有时区或 GMT 偏移量。我假设您已经打印了
Date
或执行了其他隐式调用它toString
方法的操作。我相信这是唯一一次以您的时区解释日期
。更准确地说,是 JVM 当前的默认时区。另一方面,这是不可避免的。Date.toString()
确实以这种方式运行,它获取 JVM 的时区设置并使用它来呈现要返回的字符串。您可以使用 java.time
但您不应该使用
Date
。该类设计得很糟糕,而且幸运的是早已过时了。另外,java.time(替代它的现代 Java 日期和时间 API)有一两个用于日期和时间的类,与 GMT 或 UTC 的偏移量。我现在认为 GMT 和 UTC 是同义词,严格来说它们不是。当我刚才运行这个片段时,输出是:
输出的尾随
Z
表示 UTC。链接
You can’t
First, you are asking the impossible. An old-fashioned
Date
object hasn’t got, as in cannot have a time zone or GMT offset.I suppose that you have printed the
Date
or done something else that implicitly calls ittoString
method. I believe that this is the only time that theDate
is interpreted in your time zone. More precisely in the current default time zone of your JVM. On the other hand this is unavoidable.Date.toString()
does behave in that way, it picks up the JVM’s time zone setting and uses it for rendering the string to be returned.You can with java.time
You shouldn’t use a
Date
, though. That class is poorly designed and fortunately long outdated. Also java.time, the modern Java date and time API that replaces it, has a class or two for dates and times with offset from GMT or UTC. I am considering GMT and UTC synonymous for now, strictly speaking they are not.When I ran this snippet just now, the output was:
The trailing
Z
of the output means UTC.Links
这非常简单直接。
现在日期将包含当前 GMT 时间。
This is pretty simple and straight forward.
Now date will contain the current GMT time.
以下有用的实用程序方法可用于管理 GMT 时间并节省 DST:
Useful Utils methods as below for manage Time in GMT with DST Savings:
在java 8之后,您可能想要获取如下格式的时间:
After java 8, you may want to get the formatted time as below:
以下代码将获取日期减去时区偏移量:
输出:
我的系统位于 GMT+8,因此 diffInMillis = 28800000 =
8 * 60 * 60 * 1000
。The following code will get the date minus timezone offset:
output :
My system is at GMT+8, so diffInMillis = 28800000 =
8 * 60 * 60 * 1000
。