java.util.Date 是否使用 TimeZone?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1516213/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:44:01  来源:igfitidea点击:

Is java.util.Date using TimeZone?

javadatetimezone

提问by Udi

I have 2 different computers, each with different TimeZone.

我有两台不同的电脑,每台电脑都有不同的时区。

In one computer im printing System.currentTimeMillis(), and then prints the following command in both computers: System.out.println(new Date(123456));--> 123456 stands for the number came in the currentTimeMillisin computer #1.

在一台计算机 im 打印System.currentTimeMillis(),然后在两台计算机中打印以下命令: System.out.println(new Date(123456));--> 123456 代表currentTimeMillis计算机 #1 中的数字。

The second print (though typed hardcoded) result in different prints, in both computers. why is that?

第二个打印(虽然是硬编码的)在两台计算机上产生不同的打印。这是为什么?

采纳答案by John M

How about some pedantic detail.

一些迂腐的细节怎么样。

java.util.Dateis timezone-independent. Says so right in the javadoc.

java.util.Date与时区无关。在 javadoc 中说得对。

You want something with respect to a particular timezone? That's java.util.Calendar.

你想要关于特定时区的东西吗?那是java.util.Calendar

The tricky part? When you print this stuff (with java.text.DateFormator a subclass), that involves a Calendar (which involves a timezone). See DateFormat.setTimeZone().

棘手的部分?当您打印这些东西(使用java.text.DateFormat子类)时,这涉及 Calendar (涉及时区)。请参阅DateFormat.setTimeZone()

It sure looks (haven't checked the implementation) like java.util.Date.toString() goes through a DateFormat. So even our (mostly) timezone-independent class gets messed up w/ timezones.

它确实看起来(还没有检查实现)像 java.util.Date.toString() 经历了一个 DateFormat。因此,即使是我们(主要是)与时区无关的类也会被时区搞乱。

Want to get that timezone stuff out of our pure zoneless Date objects? There's Date.toGMTString(). Or you can create your own SimpleDateFormatter and use setTimeZone() to control which zone is used yourself.

想要从我们纯粹的无区 Date 对象中获取时区内容吗?有Date.toGMTString()。或者您可以创建自己的 SimpleDateFormatter 并使用 setTimeZone() 来控制自己使用哪个区域。

回答by Brian Agnew

Because that milliseconds number is the number of milliseconds past 1/1/1970 UTC. If you then translate to a different timezone, the rendered time will be different.

因为该毫秒数是 UTC 时间 1/1/1970 过去的毫秒数。如果您随后转换到不同的时区,则渲染时间将有所不同。

e.g. 123456 may correspond to midday at Greenwich (UTC). But that will be a different time in New York.

例如,123456 可能对应于格林威治 (UTC) 的中午。但这将是纽约的不同时期。

To confirm this, use SimpleDateFormatwith a time zone output, and/or change the timezone on the second computer to match the first.

要确认这一点,请将SimpleDateFormat与时区输出一起使用,和/或更改第二台计算机上的时区以匹配第一台计算机。

回答by svens

See https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#toString().

请参阅https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#toString()

Yes, it's using timezones. It should also print them out (the three characters before the year).

是的,它使用时区。它还应该将它们打印出来(年份之前的三个字符)。

回答by Michael Borgwardt

why is that?

这是为什么?

Because something like "Oct 4th 2009, 14:20" is meaningless without knowing the timezone it refers to - which you can most likely see right now, because that's my time as I write this, and it probably differs by several hours from your time even though it's the same moment in time.

因为“2009 年 10 月 4 日,14:20”之类的内容在不知道它所指的时区的情况下毫无意义 - 您现在很可能会看到,因为这是我写这篇文章时的时间,它可能与您的时间相差几个小时即使是同一时刻。

Computer timestamps are usually measured in UTC (basically the timezone of Greenwich, England), and the time zone has to be taken into account when formatting them into something human readable.

计算机时间戳通常以 UTC(基本上是英格兰格林威治的时区)来衡量,并且在将它们格式化为人类可读的格式时必须考虑时区。

回答by lwpro2

javadoc explains this well, System.currentTimeMillis() Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

javadoc很好的解释了这一点, System.currentTimeMillis() 注意,虽然返回值的时间单位是毫秒,但值的粒度取决于底层操作系统,可能会更大。例如,许多操作系统以几十毫秒为单位测量时间。