使用gson将Java日期转换为UTC
我似乎无法让gson在java中将日期转换为UTC时间。。。。 这是我的密码使用gson将Java日期转换为UTC,java,json,date,gson,utc,Java,Json,Date,Gson,Utc,我似乎无法让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();
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));
这是我的输出
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"
很明显,我可以在调用JSON之前减去1小时,但这似乎是一个错误。如何将gson配置为始终转换为UTC?日期格式中的Z是单引号,必须取消引号才能替换为实际时区
此外,如果您希望日期为UTC,请先将其转换。经过进一步研究,这似乎是一个已知问题。gson默认序列化程序始终默认为本地时区,不允许您指定时区。请参阅以下链接 解决方案是创建自定义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);
}
}
}
现在可以正确输出UTC格式的日期
"2014-09-25T17:21:42.026Z"
感谢链接作者。对于这个问题,我的解决方案是创建一个自定义日期适配器(请小心,以便导入
java.util.Date
而不是java.sql.Date
!)
我调整了标记的日期格式,并将其参数化为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);
}
}
}
import com.google.gson.JsonDeserializationContext;
导入com.google.gson.JsonDeserializer;
导入com.google.gson.JsonElement;
导入com.google.gson.JsonParseException;
导入com.google.gson.JsonPrimitive;
导入com.google.gson.JsonSerializationContext;
导入com.google.gson.JsonSerializer;
导入java.lang.reflect.Type;
导入java.text.DateFormat;
导入java.text.ParseException;
导入java.util.Date;
公共类GsonDateFormatAdapter实现JsonSerializer,JsonDeserializer{
私人最终日期格式日期格式;
公共GsonDateFormatAdapter(日期格式日期格式){
this.dateFormat=dateFormat;
}
@凌驾
公共同步JsonElement序列化(日期、类型、JsonSerializationContext JsonSerializationContext){
返回新的JsonPrimitive(dateFormat.format(date));
}
@凌驾
公共同步日期反序列化(JsonElement JsonElement,类型类型,JSONESerializationContext JSONESerializationContext){
试一试{
return dateFormat.parse(jsonElement.getAsString());
}捕获(解析异常){
抛出新的JsonParseException(e);
}
}
}
它是有效的ISO8601。如果首先将其转换为UTC,SimpleDataFormat将在那里放置一个Z,否则偏移量为。因此,dateFormat中的Z可能应该是xxx,而引号中的Z给出了“2014-09-25T18:17:21.026+0100”,这是正确的时间,但不是UTC。不带引号的Z只添加时区+0100。我需要输出为UTC时间,准确读取“2014-09-25T17:21:42.026Z”。您必须首先将日期对象转换为UTC时区。你能给gson一个DateFormat对象而不是字符串吗?因为您可以使用sdf.setTimeZone(timezone.getTimeZone(“UTC”))告诉SimpleDataFormat对象其时区代码>不要只使用XX
而是XXX
。如果我需要它是静态的,我该怎么办?不知道你为什么说这个类不能是静态的。可能是。我将它用作静态类。SimpleDateFormat
不是线程安全的。你会有大麻烦的。。。在许多情况下,如果您意识到这一事实,这实际上不会产生任何影响。如果您可以扩展到包括commons-lang3,您可以使用线程安全格式设置程序,即。,
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 = new GsonBuilder().registerTypeAdapter(Date.class, new ColonCompatibileDateTypeAdapter()).create();
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);
}
}
}