先把"未知"替换为""

直接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

java.text.ParseException: Failed to parse date ["未知']的更多相关文章

  1. java.text.ParseException: Unparseable date: "2015-06-09 hh:56:19"

    1.错误描述 [DEBUG:]2015-06-09 16:56:19,520 [-------------------transcation start!--------------] java.te ...

  2. Java java.text.ParseException: Unparseable date

    用java将字符串转换成Date类型是,会出现java.text.ParseException: Unparseable date异常. 例如下面的这段代码就会出现上面的异常: public bool ...

  3. java.text.ParseException: Unparseable date: &quot;2015-06-09 hh:56:19&quot;

    1.错误描写叙述 [DEBUG:]2015-06-09 16:56:19,520 [-------------------transcation start!--------------] java. ...

  4. Invocation of init method failed; nested exception is java.text.ParseException: '?' can only be specfied for Day-of-Month or Day-of-Week.

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cronTrigger' ...

  5. 【Elasticsearch】ES中时间查询报错:Caused by: ElasticsearchParseException[failed to parse date field [Sun Dec 31 16:00:00 UTC 2017] with format [yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis]];

    ES中时间查询报错:Caused by: ElasticsearchParseException[failed to parse date field [Sun Dec 31 16:00:00 UTC ...

  6. 异常--java.text.ParseException: Unparseable date

    String d = "2015-05-19" SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy/MM/dd HH ...

  7. 字符串类型日期时间转换为Date类型解析转换异常java.text.ParseException: Unparseable date: “2019-09-27T18:31:31+08:00”

    错误的写法: SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //这里的格式也可以是别 ...

  8. [bug] java.text.ParseException: Unparseable date: "2020-01-01"

    原因 输入的字符串应和目标字符串格式长度都一样 如返回的字符串是:2019-11-11,但解析的格式是:yyyy-MM-dd  HH:mm:ss,导致错误 参考 https://blog.csdn.n ...

  9. Gson本地和服务器环境不同遇到的Date转换问题 Failed to parse date []: Invalid time zone indicator

    GoogleGson在处理Date格式时有个小陷阱,在不同环境中部署时可能会遇到问题. Gson默认处理Date对象的序列化/反序列化是通过一个SimpleDateFormat对象来实现的,通过下面的 ...

随机推荐

  1. 杂项-IIS:发布杂项

    ylbtech-杂项-IIS:发布杂项 1. 测试连接返回顶部 1.1.授权 无法验证对路径的访问. 1.2.详情信息 服务器配置为将传递身份验证和内置帐户一起使用,以访问指定的物理路径.但是,IIS ...

  2. mycat接oracle和mysql多个实例

    Mycat 后面接一个Oracle实例与一个MySQL实例,假设用户表,订单表,转账记录表,Oracle字符集为GBK的,MySQL字符集则要求UTF8的完成用户表与订单表到MySQL的迁移过程,要求 ...

  3. src路径

  4. MySQL 数据库 练习题

    一.表关系 请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 3.查询平均成绩大于60分的同学的学号和平均成绩: 4.查询所有 ...

  5. redis学习一 大体概述

    redis 命令查找:http://doc.redisfans.com/ 1,redis 技术简介以及疑问      redis是一个开源的,内存存储的数据结构服务器.可以用做数据库,高速缓存和消息队 ...

  6. DDD学习笔录——简介领域驱动设计的实践与原则

    DDD在存在许多DDD模式的同时,也有大量实践和指导原则,这些都是DDD思想体系成功的关键. 1.专注于核心领域 DDD强调的是在核心子域付出最多努力的需要.核心子域是你的产品会成功还是会失败的差异化 ...

  7. openGL一些概念01

    顶点数据: 顶点数据是一系列顶点的集合. 一个顶点(Vertex)是一个3d坐标的数据的集合. 而顶点数据是用顶点属性(Vertex Attribute)表示的,它可以包含任何我们想用的数据. (但是 ...

  8. 探索Web Office Apps服务

    老样子,先放几个官链: WOA部署规划:http://technet.microsoft.com/zh-cn/library/jj219435(v=office.15).aspx 拓扑规划:http: ...

  9. 学习CSS的思路(转)

    两周之前写过该系列的第一篇,其实当时只是一个想法,这段时间迟迟未更新,是在思考一个解决过程.现在初有成效,就开更吧. 1. 一个段子 开题不必太严肃,写博客也不像写书,像聊天似的写东西是最好的表达方式 ...

  10. PHP算法

    一,实现快速排序 <?php function quickSort($arr) { $len=count($arr) ; if($len<=1) { return $arr; } $key ...