简体   繁体   English

如何在 Java 中准确地将 Epoch 时间戳转换为 java.time.Instant 类型的 object?

[英]How to convert an Epoch timestamp into an object of type java.time.Instant Accurately in Java?

I have a Spring Boot JPA Application that interacts with a 3rd party API.我有一个 Spring 启动 JPA 应用程序,它与第 3 方 API 交互。 The response payload of the API has a key API的响应payload有一个key

"created_at": 1591988071 “created_at”:1591988071

I need to parse this field into java.time.Instant so that I can do some comparisons with the value I have in the Database.我需要将此字段解析为 java.time.Instant 以便我可以与数据库中的值进行一些比较。 I have learned that I can use the below mentioned piece of code.我了解到我可以使用下面提到的一段代码。

    Instant instant = Instant.ofEpochSecond(1591988071);

Output: Output:

    2020-06-12T18:54:31Z

But to be honest, this output is off by a couple of hours.但老实说,这个 output 已经关闭了几个小时。

I found another approach, wherein if I use我找到了另一种方法,如果我使用

    String dateAsText = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            .format(new Date(1591988071 * 1000L));
    System.out.println(dateAsText);

I get the desired output but in String format.我得到了所需的 output 但采用字符串格式。

    2020-06-13 00:24:31

Can someone tell me how to obtain the above String output but converted into type java.time.Instant?有人可以告诉我如何获得上述字符串 output 但转换为 java.time.Instant 类型吗?

It is likely you're in a different timezone than UTC.您所在的时区可能与 UTC 不同。 The instant is giving you time in UTC.瞬间为您提供 UTC 时间。 That's indicated by the Z at the end of your first output.这由您的第一个 output 末尾的Z表示。

You would want to look at atZone你会想看看在Zone

Instant instant = Instant.ofEpochSecond(1591988071);
System.out.println(instant);
final ZonedDateTime losAngeles = instant.atZone(ZoneId.of("America/Los_Angeles"));
System.out.println(losAngeles);
final ZonedDateTime mumbai = instant.atZone(ZoneId.of("UTC+0530"));
System.out.println(mumbai);

This gives you something you might expect这为您提供了您可能期望的东西

2020-06-12T18:54:31Z
2020-06-12T11:54:31-07:00[America/Los_Angeles]
2020-06-13T00:24:31+05:30[UTC+05:30]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将毫秒时间戳反序列化为java.time.Instant - Deserialize millisecond timestamp to java.time.Instant 将 java.time.Instant 转换为没有区域偏移量的 java.sql.Timestamp - Convert java.time.Instant to java.sql.Timestamp without Zone offset java.time.Instant 是如何计算纳秒的? - How are nanoseconds calculated in java.time.Instant? 同时将XMLGregorianCalendar转换为java.time.Instant +切换时区 - Convert XMLGregorianCalendar to java.time.Instant + switch timezone in the meanwhile 无法反序列化`java.time.Instant`类型的值 - jackson - Cannot deserialize value of type `java.time.Instant` - jackson 如何在 java.time.Instant 中获取和设置指定时间? - How to get and set specified time in java.time.Instant? 如何从MySQL DATETIME转换为java.time.Instant并在系统默认时区显示给用户? - How do I convert from MySQL DATETIME to java.time.Instant and display to user in system default timezone? 如何在 Swagger 2.x 中使用 java.time.Instant? - How to use java.time.Instant with Swagger 2.x? 如何使用 objectMapper 为 java.time.Instant 设置字符串格式? - How to set format of string for java.time.Instant using objectMapper? 如何在 Spring Webflux 中将 java.time.Instant 序列化为 ISO 字符串 - How to serialize java.time.Instant as ISO string in Spring Webflux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM