使用 GSON 将 java.time.LocalDateTime (java 8) 序列化为 js Date 的最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21981902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:49:23  来源:igfitidea点击:

Best practice to Serialize java.time.LocalDateTime (java 8) to js Date using GSON

javajavascriptserializationgson

提问by pasanbuddhika

In our recent project we use java 8. I need to serialize java.time.LocalDateTime to java script Date format.

在我们最近的项目中,我们使用 java 8。我需要将 java.time.LocalDateTime 序列化为 java 脚本日期格式。

Currently what I did was define a custom serializer to convert LocalDateTime to timestamp.

目前我所做的是定义一个自定义序列化程序来将 LocalDateTime 转换为时间戳。

public class LocalDateTimeSerializer implements JsonSerializer<LocalDateTime> {
    @Override
    public JsonElement serialize(LocalDateTime localDateTime, Type type, JsonSerializationContext jsonSerializationContext) {
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        Date date = Date.from(instant);
        return new JsonPrimitive(date.getTime());
    }
}

then create Gson object using GsonBuilder with my custom LocalDateTimeSerializer

然后使用 GsonBuilder 和我的自定义 LocalDateTimeSerializer 创建 Gson 对象

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer());

Gson gson = gsonBuilder.create();

Then in Java Script I create a Date object using this time stamp. It's working fine.

然后在 Java Script 中我使用这个时间戳创建一个 Date 对象。它工作正常。

I need to know, is this way ok or is there a better way to do this?

我需要知道,这样可以吗,还是有更好的方法来做到这一点?

采纳答案by Weibo Li

YES, that's the best way.

是的,这是最好的方法。

It's highly recommended to convert a Timeobject into it's longtype representation when you're going to transfer it from one system to another. This can avoid many problems, such as data formatting and time local in different systems.

当您要将Time对象long从一个系统转移到另一个系统时,强烈建议将其转换为它的类型表示。这样可以避免很多问题,例如不同系统中的数据格式和时间本地。

And what's more, longrepresentation takes only 8 bytes, while string representation takes a little more. Which means longrepresentation is more efficient to transfer and parse.

更重要的是,long表示只需要 8 个字节,而字符串表示需要多一点。这意味着long表示可以更有效地传输和解析。

回答by vladaman

Java 8 solution from LocalDateTime to Epoch Milliseconds or Seconds:

从 LocalDateTime 到 Epoch 毫秒或秒的 Java 8 解决方案:

// to Epoch Seconds
long sec = localDateTime.toInstant(ZoneOffset.UTC).getEpochSecond();

// to Epoch Milliseconds
long msec = localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli();

In your case however I see a bug which uses Local Timezone instead of UTC. My recommended solution would be:

但是,在您的情况下,我看到一个使用本地时区而不是 UTC 的错误。我推荐的解决方案是:

@Override
public JsonElement serialize(LocalDateTime localDateTime, Type type, JsonSerializationContext jsonSerializationContext) {
    long sec = localDateTime.toInstant(ZoneOffset.UTC).getEpochSecond();
    return new JsonPrimitive(sec);
}