GsonBuilder setDateFormat for“2011-10-26T20:29:59-07:00” [英] GsonBuilder setDateFormat for "2011-10-26T20:29:59-07:00"

查看:103
本文介绍了GsonBuilder setDateFormat for“2011-10-26T20:29:59-07:00”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在json中获取日期/时间为 2011-10-26T20:29:59-07:00 。使用 gsonBuilder.setDateFormat 来正确格式化这个时间的正确方法是什么? -07:00 是ISO 8601时区表示法。

。这不被 SimpleDateFormat 支持,直到 Java 7 。因此,如果您可以升级到Java 7,那么您可以使用 X 来表示该时区表示法:

  Gson gson =新的GsonBuilder()。setDateFormat(yyyy-MM-dd'T'HH:mm:ssX)。create(); 

Java 6 ,您需要先进行一些模式匹配并替换JSON字符串以替换 -07: 00 部分由RFC 822标记 -0700 ,以便您可以使用 Z

  Gson gson = new GsonBuilder()。setDateFormat(yyyy-MM-dd'T'HH:mm:ssZ)。创建(); 

或一般时区记法 GMT-07:00 以便您可以使用 z

  Gson gson = new GsonBuilder()。setDateFormat(yyyy-MM-dd'T'HH:mm:ssz)。create(); 


I'm getting a date/time in json as 2011-10-26T20:29:59-07:00. What's the proper way to use gsonBuilder.setDateFormat to properly format this time?

解决方案

The -07:00 is the ISO 8601 time zone notation. This is not supported by SimpleDateFormat until Java 7. So, if you can upgrade to Java 7, then you can use the X to represent that time zone notation:

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssX").create();

On Java 6 you would need to do some pattern matching and replacing on the JSON string first to replace the -07:00 part by the RFC 822 notation -0700 so that you can use Z:

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();

or by the general time zone notation GMT-07:00 so that you can use z:

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssz").create();

这篇关于GsonBuilder setDateFormat for“2011-10-26T20:29:59-07:00”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆