使用 gson 将 Java 日期转换为 UTC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26044881/
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-11 01:45:34  来源:igfitidea点击:

Java Date to UTC using gson

javajsondategsonutc

提问by Marky0

I can't seem to get gson to convert a Date to UTC time in java.... Here is my code...

我似乎无法让 gson 在 java 中将 Date 转换为 UTC 时间......这是我的代码......

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create();
//This is the format I want, which according to the ISO8601 standard - Z specifies UTC - 'Zulu' time

Date now=new Date();          
System.out.println(now);       
System.out.println(now.getTimezoneOffset());
System.out.println(gson.toJson(now));

Here is my Output

这是我的输出

Thu Sep 25 18:21:42 BST 2014           // Time now - in British Summer Time 
-60                                    // As expected : offset is 1hour from UTC    
"2014-09-25T18:21:42.026Z"             // Uhhhh this is not UTC ??? Its still BST !!

The gson result I want and what I was expecting

我想要的 gson 结果和我所期待的

"2014-09-25T17:21:42.026Z"

I can clearly just subtract 1hr before the call toJson but this seems to be a hack. How can I configure gson to always convert to UTC ?

我显然可以在调用 toJson 之前减去 1 小时,但这似乎是一个黑客。如何配置 gson 以始终转换为 UTC ?

采纳答案by Marky0

After some further research, it appears this is a known issue. The gson default serializer always defaults to your local timezone, and doesn't allow you to specify the timezone. See the following link.....

经过一些进一步的研究,这似乎是一个已知问题。gson 默认序列化程序始终默认为您的本地时区,并且不允许您指定时区。请看以下链接.....

https://code.google.com/p/google-gson/issues/detail?id=281

https://code.google.com/p/google-gson/issues/detail?id=281

The solution is to create a custom gson type adaptor as demonstrated in the link:

解决方案是创建一个自定义的 gson 类型适配器,如链接中所示:

// this class can't be static
public class GsonUTCDateAdapter implements JsonSerializer<Date>,JsonDeserializer<Date> {

    private final DateFormat dateFormat;

    public GsonUTCDateAdapter() {
      dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);      //This is the format I need
      dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));                               //This is the key line which converts the date to UTC which cannot be accessed with the default serializer
    }

    @Override public synchronized JsonElement serialize(Date date,Type type,JsonSerializationContext jsonSerializationContext) {
        return new JsonPrimitive(dateFormat.format(date));
    }

    @Override public synchronized Date deserialize(JsonElement jsonElement,Type type,JsonDeserializationContext jsonDeserializationContext) {
      try {
        return dateFormat.parse(jsonElement.getAsString());
      } catch (ParseException e) {
        throw new JsonParseException(e);
      }
    }
}

Then register it as follows :

然后注册如下:

  Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new GsonUTCDateAdapter()).create();
  Date now=new Date();
  System.out.println(gson.toJson(now));

This now correctly outputs the Date in UTC

这现在可以正确输出 UTC 日期

"2014-09-25T17:21:42.026Z"

Thanks go to the link author.

感谢去链接作者。

回答by sleeplessnerd

The Z in your dateformat is in single-quotes, it must be unquoted to be replaced by the actual timezone.

日期格式中的 Z 是单引号,它必须不加引号才能被实际时区替换。

Furthermore, if you want your date in UTC, convert it first.

此外,如果您希望以 UTC 格式显示日期,请先将其转换。

回答by martar

The solution that worked for me for this issue was to create a custom Date adapter (P.S be carefull so that you import java.util.Datenot java.sql.Date!)

针对这个问题对我有用的解决方案是创建一个自定义日期适配器(请注意java.util.Date不要导入java.sql.Date!)

public class ColonCompatibileDateTypeAdapter implements JsonSerializer<Date>, JsonDeserializer< Date> {
private final DateFormat dateFormat;

public ColonCompatibileDateTypeAdapter() {
  dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ") {
        @Override
        public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos) {
            StringBuffer rfcFormat = super.format(date, toAppendTo, pos);
            return rfcFormat.insert(rfcFormat.length() - 2, ":");
        }

        @Override
        public Date parse(String text, ParsePosition pos) {
            if (text.length() > 3) {
                text = text.substring(0, text.length() - 3) + text.substring(text.length() - 2);
            }
            return super.parse(text, pos);
        }
    };


}

@Override public synchronized JsonElement serialize(Date date, Type type,
    JsonSerializationContext jsonSerializationContext) {
  return new JsonPrimitive(dateFormat.format(date));
}

@Override public synchronized Date deserialize(JsonElement jsonElement, Type type,
    JsonDeserializationContext jsonDeserializationContext) {
  try {
      return dateFormat.parse(jsonElement.getAsString());
  } catch (ParseException e) {
    throw new JsonParseException(e);
  }
}}

and then use it while creating GSON object

然后在创建 GSON 对象时使用它

Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new ColonCompatibileDateTypeAdapter()).create();

回答by Ferran Maylinch

I adapted the marked solutionand parametrized the DateFormat:

我调整了标记的解决方案并参数化了DateFormat

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

public class GsonDateFormatAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {

    private final DateFormat dateFormat;

    public GsonDateFormatAdapter(DateFormat dateFormat) {
        this.dateFormat = dateFormat;
    }

    @Override
    public synchronized JsonElement serialize(Date date, Type type, JsonSerializationContext jsonSerializationContext) {
        return new JsonPrimitive(dateFormat.format(date));
    }

    @Override
    public synchronized Date deserialize(JsonElement jsonElement, Type type,JsonDeserializationContext jsonDeserializationContext) {
        try {
            return dateFormat.parse(jsonElement.getAsString());
        } catch (ParseException e) {
            throw new JsonParseException(e);
        }
    }
}