如何将时间戳从GMT转换为Unix时期
我正在解析“ 2022-01-12T17:17:34.512492+0000” 的时间戳。 sssss'zzzz“ (ISO8601)。
我想在epoch unix时间转换它,我正在使用java.text.simpledateformat。 我尝试了两种方法,但两者都不起作用:
1-第一个方法
val parsed = "2022-01-12T17:17:34.512492+0000"
val df: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'ZZZZ'")
val date = df.parse(parsed.toString)
val epoch = date.getTime
错误显示:
java.text.ParseException: Unparseable date: "2022-01-12T17:17:34.512492+0000"
2-第二种方法显示了输出,但
val parsed = "2022-01-12T17:17:34.512492+0000"
val df: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'+0000'")
val date = df.parse(parsed.toString)
val epoch = date.getTime
println(epoch)
输出不正确:
1642004766492
如果将此时代转换为人类日期:” 2022年1月12日,星期三16 :26:06.492“
小时,分钟和秒是错误的。
I'm parsing a timestamp which is "2022-01-12T17:17:34.512492+0000", this format is "yyyy-MM-dd'T'HH:mm:ss.SSSSSS'ZZZZ" (ISO8601).
I want to convert it in epoch unix time, I'm using java.text.SimpleDateFormat.
I tried two methods but both don't work:
1- First Method
val parsed = "2022-01-12T17:17:34.512492+0000"
val df: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'ZZZZ'")
val date = df.parse(parsed.toString)
val epoch = date.getTime
Error showed:
java.text.ParseException: Unparseable date: "2022-01-12T17:17:34.512492+0000"
2- This second Method shows an output but is incorrect
val parsed = "2022-01-12T17:17:34.512492+0000"
val df: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'+0000'")
val date = df.parse(parsed.toString)
val epoch = date.getTime
println(epoch)
Output:
1642004766492
If you convert this epoch to HUMAN DATE: "Wednesday, 12 January 2022 16:26:06.492"
The hours,minutes and seconds are wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如Gael指出的那样,
SimpleDateFormat
已过时。API现在支持纳秒秒,因此微秒并不是这里的问题。您应该使用 datement formatter 代码> ZonedDateTime 。您的模式有点错。检查
偏移Z
的文档:您还可以使用
toinstant
打印时间以确保正确解析它:这是不错的文章 详细解释了Java中ISO 8601的详细说明。最后的评论特别有用,因为它显示了所使用的不同模式之间的差异。
SimpleDateFormat
is outdated, as Gael pointed out.The Time API now supports up to nanoseconds, so microseconds are not an issue here. You should use DateTimeFormatter with
ZonedDateTime
. Your pattern is slightly wrong. Checking the docs forOffset Z
:You can also print the time using
toInstant
to make sure it was parsed correctly:Here is a nice article that explains in detail converting an ISO 8601 in Java. The comments at the end are particularly useful, as it shows the difference between the different patterns used.
看起来Epoch具有内部DateTime格式的数据。
您应该将其转换为字符串格式
像这样在爪哇
looks like epoch has data in internal datetime format.
and you should convert it to string format
like this in java
您拥有微秒精度的时间戳。
SimpleDateFormat
不支持这种精度。同样,Unix时期时间通常达到毫秒精度。这里可能的解决方案是在解析之前将微秒明确到字符串中的毫秒到毫秒,然后使用
yyyy-mm-dd'hh:mm:mm:ss.sssz
格式。The timestamp you have has microseconds precision. Such precision is not supported by
SimpleDateFormat
. Also, Unix epoch time is usually up to milliseconds precision.Possible solution here is to explicitly round the microseconds to milliseconds in the string before parsing, then use the
yyyy-MM-dd'T'HH:mm:ss.SSSZ
format.