【问题标题】:How can I convert from a String to Epoch microseconds?如何从字符串转换为纪元微秒?
【发布时间】:2019-12-27 23:15:35
【问题描述】:

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

这是我现在的代码:

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))

但这给了我这个例外:

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")),我会得到相同的结果。

感谢您的帮助!

【问题讨论】:

  • 你试过用ISO_OFFSET_DATE_TIME解析吗?
  • @chrylis 刚试过这个,当我添加 .withZone(ZoneId.systemDefault()) 时它是正确的.. 奇怪
  • 如果我指定了时区,我只能在服务器上进行这项工作。我希望它只适用于偏移量。
  • 如果将日期转换为时间戳,则会丢失(原始)时区的信息。唯一明智的选择是以 UTC 显示解码的时间戳。

标签: java timestamp datetime-format zone instant


【解决方案1】:

您的微秒值是正确的。

发生的情况是: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

【讨论】:

  • 感谢您的解释!时区可能会很混乱。 OffsetDateTime 最终确实对我有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
  • 1970-01-01
相关资源
最近更新 更多