在Java中将纪元秒转换为日期和时间格式
我有自 1970 年 1 月 1 日 UTC(大纪元时间)以来的秒数。
1320105600
我需要将秒数转换为以下格式的日期和时间。
Friday,November 4,2011 5:00,AM
我怎样才能实现这个目标?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您仅限于旧版
java.util.Date
和java.util.Calendar
API,则需要考虑到时间戳是以毫秒为单位解释的,而不是秒。因此,您首先需要将其乘以 1000 以获得以毫秒为单位的时间戳。通过这种方式,您可以将其提供给
java.util 的构造函数。日期
并最终使用SimpleDateFormat
转换a将
java.util.Date
转换为java.lang.String
以所需的日期格式模式,如有必要,使用预定义时区(否则将使用系统 默认时区,本身不是 GMT/UTC,因此格式化的时间可能已关闭)。如果您已经使用 Java8,可以使用
LocalDateTime#ofEpochSecond()
它允许您直接提供纪元秒,而不需要乘以毫秒风格。In case you're restricted to legacy
java.util.Date
andjava.util.Calendar
APIs, you need to take into account that the timestamps are interpreted in milliseconds, not seconds. So you first need to multiply it by 1000 to get the timestamp in milliseconds.This way you can feed it to a.o. the constructor of
java.util.Date
and finally useSimpleDateFormat
to convert ajava.util.Date
tojava.lang.String
in the desired date format pattern, if necessary with a predefined time zone (otherwise it would use the system default time zone, which is not GMT/UTC per se and thus the formatted time might be off).In case you're already on Java8, there's a
LocalDateTime#ofEpochSecond()
which allows you to feed epoch seconds directly without the need for multiplying into milliseconds flavor.请参阅此 javadoc了解更多信息。构造函数需要毫秒。
要以适当的格式显示此日期,您应该检查 DateFormat< /a>
这是一个例子:
See this javadoc for more info. The constructor needs milliseconds.
To display this date in an appropriate format you should check DateFormat
Here is an example:
java.time
BalusC的回答很好,因为它指出您使用java.time。但该答案使用
LocalDateTime
< /a> 其中即时
< /a> 更合适。LocalDateTime
不是时间线上的一个时刻,因为它故意没有 offset-from- 的概念UTC 或时区。java.time
java.time框架内置于 Java 8 及更高版本中。这些类取代了旧的麻烦的日期时间类,例如 java.util.Date、.Calendar 和 & 。
java.text.SimpleDateFormat
。 Joda-Time 团队还建议迁移到 java.time。要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。
许多 java.time 功能都向后移植到 Java 6 和 Java 6。 ThreeTen-Backport 中的 7.0 并在 ThreeTenABP。
即时
UTC 时间线上的某个时刻,分辨率高达 < a href="https://en.wikipedia.org/wiki/Nanosecond" rel="noreferrer">纳秒 由
Instant
类。转储到控制台。您的输入值是 2011 年 11 月 1 日 UTC 的第一时刻。末尾的
Z
是“Zulu”的缩写,表示 UTC。ZonedDateTime
在您的评论中,您提到希望通过
美国/芝加哥
时区。使用正确的时区名称。应用时区ZoneId
来获取ZonedDateTime
对象。我们看到芝加哥在该日期比 UTC 晚五个小时。字符串
上面看到的字符串是标准的 ISO 8601 格式。要生成其他格式的字符串,请使用
DateTimeFormatter
类。您可以指定自己的自定义模式。但通常最好让 java.time 自动本地化为Locale
对象。要指定您自己的自定义格式,请搜索 Stack Overflow 以获取许多示例和更多讨论。
java.time
The Answer by BalusC is good in that it points you to using java.time. But that Answer uses
LocalDateTime
whereInstant
is more appropriate. ALocalDateTime
is not a moment on the timeline as it purposely has no concept of offset-from-UTC or time zone.java.time
The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as
java.util.Date
,.Calendar
, &java.text.SimpleDateFormat
. The Joda-Time team also advises migration to java.time.To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.
Instant
A moment on the timeline in UTC with a resolution up to nanoseconds is represented by the
Instant
class.Dump to console. Your input value is the first moment of November 1, 2011 in UTC. The
Z
on the end, short for 'Zulu', means UTC.ZonedDateTime
In your comments you mention wanting to see this date-time through the lens of the
America/Chicago
time zone. Use a proper time zone name. Apply a time zone,ZoneId
, to get aZonedDateTime
object. We see that Chicago is five hours behind UTC on that date.Strings
The Strings seen above are in standard ISO 8601 format. To generate strings in other formats, use the
DateTimeFormatter
class. You can specify your own custom pattern. But generally best to let java.time automatically localize to the human language and cultural norms encoded in aLocale
object.To specify your own custom format, search Stack Overflow for many examples and more discussion.
诀窍是使用 java.util.Date 和 java.text.DateFormat 来获取您想要的格式。您可以在网络教程中查找操作方法。
The trick is to use
java.util.Date
andjava.text.DateFormat
to get the format you want. You can look up how to do it in tutorials on the Web.