今天在用Gson做json转化是遇到一个问题,本地执行没有问题(windows 7),包丢到服务器上(Centos)就报错了。
后经分析发现DateTypeDapter类中取本地环境的日期格式参考http://blog.csdn.net/liao_leo/article/details/44593095:
private final DateFormat enUsFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.US); private final DateFormat localFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT); private final DateFormat iso8601Format = buildIso8601Format(); private static DateFormat buildIso8601Format() { DateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd‘T‘HH:mm:ss‘Z‘", Locale.US); iso8601Format.setTimeZone(TimeZone.getTimeZone("UTC")); return iso8601Format; }
解决方法:
修改Gson gson = new Gosn();
为:Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
Bingo!