代码之家  ›  专栏  ›  技术社区  ›  Anuj Mehta

Java8:如何将时间戳转换为长时间

  •  1
  • Anuj Mehta  · 技术社区  · 7 年前

    我正在编写一个后端服务并接收时间戳,如下所示

    Instant timestamp = Instant.now().plusMillis(offset);
    bodyObject.put("timestamp", DateTimeFormatter.ISO_INSTANT.format(timestamp));
    

    现在在我的服务端,我想把这个时间戳转换成 "Long" 用于进一步处理的数据类型。请指导我如何才能做到这一点?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Anuj Mehta    7 年前

    我正在将时间戳格式转换为 Instant getEpochSecond() 方法获取秒数

    Instant instant = Instant.from(DateTimeFormatter.ISO_INSTANT.parse(format));
    instant.getEpochSecond();