首页 > 解决方案 > Java 8:如何将时间戳转换为长

问题描述

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

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

现在在我的服务端,我想将此时间戳转换为"Long"数据类型以供进一步处理。请指导我如何实现这一目标?

标签: java-8timestamp

解决方案


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

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

推荐阅读