使用 google gson 转换Timestamp或Date类型为JSON字符串.
2013-01-18 18:14
567 查看
转http://joesmart.iteye.com/blog/564633
gson lib 包为1.4版本.
创建类型适配类:
Timestamp类型适配类代码
import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
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;
public class TimestampTypeAdapter implements JsonSerializer<Timestamp>, JsonDeserializer<Timestamp>{
private final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public JsonElement serialize(Timestamp src, Type arg1, JsonSerializationContext arg2) {
String dateFormatAsString = format.format(new Date(src.getTime()));
return new JsonPrimitive(dateFormatAsString);
}
public Timestamp deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (!(json instanceof JsonPrimitive)) {
throw new JsonParseException("The date should be a string value");
}
try {
Date date = format.parse(json.getAsString());
return new Timestamp(date.getTime());
} catch (ParseException e) {
throw new JsonParseException(e);
}
}
}
类型适配类
应用类型适配器 写道
Gson gson = new GsonBuilder().registerTypeAdapter(Timestamp.class,new TimestampTypeAdapter()).setDateFormat("yyyy-MM-dd HH:mm:ss").create();
String jsonString = gson.toJson(resourceInfo,ResourceGeoInfo.class);
输出结果
{"positionTime":"2010-01-07 10:57:27"}
Date 类型的时间转换第二种方式;
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
String jsonString = gson.toJson(new Date(System.currentTimeMillis()),Date.class);
System.out.println(jsonString);
输出结果:
"2010-01-07 12:24:34"
gson lib 包为1.4版本.
创建类型适配类:
Timestamp类型适配类代码
import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
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;
public class TimestampTypeAdapter implements JsonSerializer<Timestamp>, JsonDeserializer<Timestamp>{
private final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public JsonElement serialize(Timestamp src, Type arg1, JsonSerializationContext arg2) {
String dateFormatAsString = format.format(new Date(src.getTime()));
return new JsonPrimitive(dateFormatAsString);
}
public Timestamp deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (!(json instanceof JsonPrimitive)) {
throw new JsonParseException("The date should be a string value");
}
try {
Date date = format.parse(json.getAsString());
return new Timestamp(date.getTime());
} catch (ParseException e) {
throw new JsonParseException(e);
}
}
}
类型适配类
应用类型适配器 写道
Gson gson = new GsonBuilder().registerTypeAdapter(Timestamp.class,new TimestampTypeAdapter()).setDateFormat("yyyy-MM-dd HH:mm:ss").create();
String jsonString = gson.toJson(resourceInfo,ResourceGeoInfo.class);
输出结果
{"positionTime":"2010-01-07 10:57:27"}
Date 类型的时间转换第二种方式;
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
String jsonString = gson.toJson(new Date(System.currentTimeMillis()),Date.class);
System.out.println(jsonString);
输出结果:
"2010-01-07 12:24:34"
相关文章推荐
- [转]使用 google gson 转换Timestamp或Date类型为JSON字符串.
- 使用 google gson 转换Timestamp或Date类型为JSON字符串.
- 使用 google gson 转换Timestamp为JSON字符串
- JSON(3):Java的Date类型转换为符合json语法的字符串遇到的问题及其解决办法
- 将java.util.Date类型转换成json时,使用JsonValueProcessor将date转换成希望的类型
- android开发过程中将json日期字符串,转换成JAVA中Date类型格式化字符串
- Oracle 字符串转换TIMESTAMP类型或Date类型
- fastjson 使用 string字符串转换成java的各种类型
- Oracle 字符串转换TIMESTAMP类型或Date类型
- 将Date日期类型的字段转换成json字符串
- 将java.util.Date类型转换成json时,使用JsonValueProcessor将date转换成希望的类型
- Android使用Google Gson实现JSON字符串和对象、对象数组之间相互转换
- 在java中实现日期类型和字符串类型的转换_所有(Date String Timestamp Datetime)
- Oracle 字符串转换TIMESTAMP类型或Date类型
- 关于如何将含有Date类型属性的对象转换成符合实际应用的Json字符串
- 将java.util.Date类型转换成json时,使用JsonValueProcessor将date转换成希望的类型
- 将java.util.Date类型转换成json时,使用JsonValueProcessor将date转换成希望的类型
- 后台date类型转换为json字符串时,返回前台页面的是long time值问题解决
- 字符串格式格林威治时间转换为Date类型的方法
- 字符串转换为Date类型