先把"未知"替换为""

直接new 出来的Gson 对象是无法解析为""的Date属性的,需要通过GsonBuilder来进行创建

Gson ignoreDateGson=new GsonBuilder().registerTypeAdapterFactory(new DateNullAdapterFactory<>()).create();

这个registerTypeAdapterFactory()方法就是添加自己的适配器,来对某些特定的类型进行处理.new 出来的这个DateNullAdapterFactory.class 需要自己写.

import java.util.Date;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken; public class DateNullAdapterFactory<T> implements TypeAdapterFactory { @SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Class<T> rawType = (Class<T>) type.getRawType();
if (rawType != Date.class) {
return null;
}
return (TypeAdapter<T>) new DateNullAdapter();
}
}
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.Date;
import java.util.Locale; import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.bind.DateTypeAdapter;
import com.google.gson.internal.bind.util.ISO8601Utils;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.zxtc.common.utils.StringUtils; public class DateNullAdapter extends TypeAdapter<Date>{ public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
return typeToken.getRawType() == Date.class ? (TypeAdapter<T>) new DateTypeAdapter() : null;
}
}; private final DateFormat enUsFormat
= DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.US);
private final DateFormat localFormat
= DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT); @Override public Date read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
String jsonStr = in.nextString();
if(StringUtils.isBlank(jsonStr)) {
return null;
}else {
return deserializeToDate(jsonStr);
}
} private synchronized Date deserializeToDate(String json) {
try {
return localFormat.parse(json);
} catch (ParseException ignored) {
}
try {
return enUsFormat.parse(json);
} catch (ParseException ignored) {
}
try {
return ISO8601Utils.parse(json, new ParsePosition(0));
} catch (ParseException e) {
throw new JsonSyntaxException(json, e);
}
} @Override public synchronized void write(JsonWriter out, Date value) throws IOException {
if (value == null) {
out.nullValue();
return;
}
String dateFormatAsString = enUsFormat.format(value);
out.value(dateFormatAsString);
} }

可能还会出现报错:Invalid time zone indicator

两种结合:把date改string

最新文章

  1. css3实践之图片轮播(Transform,Transition和Animation)
  2. CLR via C#(07)-静态类,分部类
  3. form表单那点事儿(下) 进阶篇
  4. MBR 基础
  5. HDU 5353
  6. PHP简单利用token防止表单重复提交(转)
  7. 如何将windows版的vim界面语言(默认为中文)设置成英文
  8. How to find variable is empty in shell script
  9. MYSQL-用户权限的验证过程(转)
  10. 设置DIV根据内容自动调整高度的三个方法
  11. vb.net 总结
  12. adobe acrobat pro 9破解方法
  13. 最新数组方法(包括es6)
  14. Python编程从入门到实践笔记——类
  15. 2018 CCPC网络赛 hdu6444 Neko&#39;s loop
  16. Java基础实践一:for关键字的实现原理
  17. java JDBC (六) org.apache.commons.dbutils 增删改
  18. php -- 获取函数参数
  19. ubuntu服务器安装FTP服务
  20. 水晶报表自定义纸张大小打印 (Crystal Report Print with custom paper size)

热门文章

  1. Dubbo各种协议详解
  2. Regexp:template
  3. pthread_cond_wait 详解
  4. 分布式爬虫搭建系列 之一------python安装及以及虚拟环境的配置及scrapy依赖库的安装
  5. 安装 MongoDB。
  6. Android Tombstone 分析
  7. .Net时间运算 - DateTime类,TimeSpan类
  8. round四舍五入
  9. Express响应方法
  10. gearman client的doBackground 与doNormal方法的区别