gpt4 book ai didi

java - 如何从字符串转换为 Epoch 微秒?

转载 作者:行者123 更新时间:2023-12-02 01:22:25 29 4
gpt4 key购买 nike

我有一个以下格式的字符串:“2019-08-17T09:51:41.775+00:00”。我需要将其转换为纪元微秒,但我的转换总是相差一个小时。

这是我目前的代码:

String timestamp = "2019-08-17T09:51:41.775+00:00"
ZonedDateTime date = ZonedDateTime.parse(timestamp)
Long epoch = date.toInstant().toEpochMilli() * 1000

因此,当我运行此代码时,我得到结果 1566035501775000。现在,如果我将 1566035501775 放入这样的转换器中: https://www.freeformatter.com/epoch-timestamp-to-date-converter.html要反转它,我得到这个日期:8/17/2019,上午 10:51:41。

为什么休息一小时?我该如何更改?

我还尝试使用 DateTimeFormatter:

DateTimeFormatter pat = DateTimeFormatter.ofPattern("YYYY-MM-dd'T'hh:mm:ss.SSSXXX")
Instant.from(pat.parse(timestamp))

但这给了我这个异常(exception):

java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {SecondOfMinute=41, MicroOfSecond=775000, DayOfMonth=17, HourOfAmPm=9, NanoOfSecond=775000000, MonthOfYear=8, OffsetSeconds=0, MinuteOfHour=51, MilliOfSecond=775, WeekBasedYear[WeekFields[SUNDAY,1]]=2019},ISO of type java.time.format.Parsed

at java.time.Instant.from(Instant.java:378)
at com.amazon.hailstonetracing.controller.chrome.WorkflowEventMapperTest.test(WorkflowEventMapperTest.kt:114)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: InstantSeconds
at java.time.format.Parsed.getLong(Parsed.java:203)
at java.time.Instant.from(Instant.java:373)
... 24 more

经过研究,我认为这是因为我没有定义 ZoneId?我只有时间戳字符串的偏移量。但如果我在 DateTimeFormatter 上指定 .withZone(ZoneId.of("UTC")) ,我会得到相同的结果。

感谢您的帮助!

最佳答案

您的微秒值是正确的。

发生的情况是:freeformatter.com Epoch & Unix Timestamp Converter 检测您的时区并为您提供本地时间,上午 10:51:41。显然,您所在的时区(或者您的浏览器认为您所在的时区)与 UTC 的偏移量为 +01:00。因此,与字符串中的时间(指定为偏移量 +00:00)相比,它增加了一个小时。

我位于欧洲/哥本哈根时区,当前偏移量为 +02:00,当我在 freeformatter.com 上尝试相同操作时,我得到 17.8.2019 11.51.41,这是我所在时区的正确本地时间。

在尝试使用显式格式化程序时,您的一些格式模式字母的大小写错误。由于它无需构建您自己的格式化程序即可工作,这当然是我推荐的解决方案。

而且您不需要时区 ID。字符串中的偏移量是完全足够的。

由于您的字符串有偏移量 (+00:00) 并且没有时区(例如太平洋/塔拉瓦),因此使用 ZonedDateTime 是多余的。我建议使用 OffsetDateTime 代替。它以同样的方式进行:

    OffsetDateTime date = OffsetDateTime.parse(timestamp);
long epoch = date.toInstant().toEpochMilli() * 1000;

我还将 epoch 的类型从 Long 对象更改为原始 long

顺便说一句,如果您的字符串具有比毫秒更精细的精度,那么它会在转换中丢失。这是一种获得微秒精度的方法:

    String timestamp = "2019-08-17T09:51:41.7754321+00:00";
OffsetDateTime date = OffsetDateTime.parse(timestamp);
Instant asInstant = date.toInstant();
long epoch = TimeUnit.SECONDS.toMicros(asInstant.getEpochSecond())
+ asInstant.get(ChronoField.MICRO_OF_SECOND);
System.out.println(epoch);

如您所见,我在您的字符串中添加了更多小数以进行演示。输出为:

1566035501775432

关于java - 如何从字符串转换为 Epoch 微秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57612574/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com