使用gson将Java日期转换为UTC

47

我似乎无法让Gson在Java中将日期转换为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 !!

我想要的gson结果和我的期望结果
"2014-09-25T17:21:42.026Z"

我可以在调用toJson之前明确地减去1小时,但这似乎是一种hack方法。我该如何配置Gson始终转换为UTC?

5个回答

112
经过进一步的研究,发现这是一个已知的问题。Gson默认序列化器始终默认为本地时区,并且不允许您指定时区。请参见以下链接.....

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

解决方案是创建一个自定义的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);
      }
    }
}

然后按以下方式注册:

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

现在以UTC格式正确输出日期

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

感谢链接作者。

如果我需要它是静态的,该怎么做? - Zapnologica
2
不确定为什么您说这个类不能是静态的。它可以是静态的。我正在将其用作静态类。 - Johann
SimpleDateFormat 不是线程安全的。如果不注意这一点,可能会遇到严重问题... 在许多情况下,只要您意识到这一事实,它就不会真正产生任何影响。 - desperateCoder
如果您可以扩展以包括commons-lang3,则可以使用线程安全的格式化程序,即FastDateFormat - VPZ

4

你的日期格式中的Z在单引号中,必须去掉引号才能被实际时区替换。

此外,如果您希望使用UTC时间,先进行转换。


这是一个有效的 ISO8601 格式。如果您先将其转换为 UTC,SimpleDateFormat 将会在后面加上 Z,否则是偏移量。http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html - sleeplessnerd
所以dateFormat中的Z应该改为XX。 - sleeplessnerd
不带引号的Z会给出“2014-09-25T18:17:21.026+0100”,这是正确的时间但不是UTC。不带引号的Z只是添加了时区+0100。我需要输出为UTC时间,并且确切地读取为“2014-09-25T17:21:42.026Z”。 - Marky0
你必须先将日期对象转换为UTC时区。你可以给gson一个DateFormat对象而不是一个字符串吗?因为你可以通过sdf.setTimeZone(TimeZone.getTimeZone("UTC"));告诉SimpleDateFormat对象它的时区。 - sleeplessnerd
1
不要使用 XX,只能使用XXX - Michael-O

2
这个问题的解决方案是创建一个自定义的日期适配器(P.S 要小心导入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);
  }
}}

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

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

1

我修改了标记solution并对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);
        }
    }
}

0

在 Kotlin 中,所有的答案都对我无效,所以我自己实现了一个:

val SERVER_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss" // Adjust as per your use case

val gson = GsonBuilder()
    .setDateFormat(SERVER_DATE_FORMAT)
    .registerTypeAdapter(Date::class.java, object : JsonSerializer<Date> {
        private val utcDateFormat = SimpleDateFormat(
            SERVER_DATE_FORMAT,
            Locale.ENGLISH
        ).apply {
            timeZone = TimeZone.getTimeZone("UTC")
        }

        // Avoid using local date formatter (with timezone) to send UTC date
        override fun serialize(src: Date?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement {
            return JsonPrimitive(src?.let { utcDateFormat.format(it) })
        }
    })
    .create()
val gsonConverterFactory = GsonConverterFactory.create(gson)

val retrofit = Retrofit.Builder()
    .baseUrl("Your URL")
    .addConverterFactory(gsonConverterFactory)
    .build()

我希望这对其他人有所帮助!


我强烈建议您不要使用SimpleDateFormatDate。这些类设计得很差,而且早已过时,前者尤其麻烦。相反,请使用来自java.time,现代Java日期和时间APILocalDateTimeDateTimeFormatter。所以说,说实话,我希望这对其他人没有用处。 - Ole V.V.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接