Java Gson-不可解析日期

Java Gson-不可解析日期,java,gson,Java,Gson,我必须将Json对象与具有日期的java对象绑定。my Json上的日期格式如下: 2013-01-04T10:50:26+0000 我使用的GsonBulder如下: Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create(); 但我得到了以下例外: The RuntimeException could not be mapped to a response, re-throwing to th

我必须将Json对象与具有日期的java对象绑定。my Json上的日期格式如下:

2013-01-04T10:50:26+0000

我使用的GsonBulder如下:

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
但我得到了以下例外:

The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
com.google.gson.JsonSyntaxException: 2013-01-04T10:50:26+0000
at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:81)
.....
Caused by: java.text.ParseException: Unparseable date: "2013-01-04T10:50:26+0000"
at java.text.DateFormat.parse(DateFormat.java:337)
at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:79)
我正在尝试使用Gson加载此请求:

https://graph.facebook.com/me?fields=id,username,email,link,updated_time&access_token=accessToken
答案是

{"id":"12345","username":"myusername","email":"myemail\u0040yahoo.it","link":"http:\/\/www.facebook.com\/mysusername","updated_time":"2013-01-04T10:50:26+0000"}

反序列化失败,因为json字符串中缺少引号

以下工作:

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

    String date = "\"2013-02-10T13:45:30+0100\"";
    Date test = gson.fromJson(date, Date.class);
    System.out.println("date:" + test);
输出:

日期:2013年2月10日星期日13:45:30 CET

编辑完整示例:

import java.util.Date;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;


public class FacebookResponse {

int id;
String username;
String email;
String link;
Date updated_time;

@Override
public String toString() {
    return "ID: " + id + " username: " + username + " email: " + email + " link: " + link + " updated_time: " + updated_time;
};



/**
 * @param args
 */
public static void main(String[] args) {        
    String json = "{\"id\":\"12345\",\"username\":\"myusername\",\"email\":\"myemail\u0040yahoo.it\",\"link\":\"http://www.facebook.com/mysusername\",\"updated_time\":\"2013-01-04T10:50:26+0000\"}";
    Gson gson=  new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();

    FacebookResponse response = gson.fromJson(json, FacebookResponse.class);
    System.out.println(response);
}

}

时间字符串(
2013-01-04T10:50:26+0000
)从何而来?我更新了问题,请看一看。谢谢Dominik,最后我的代码正常工作了,但我用格式错误的json测试代码。感谢您的反馈有没有办法告诉setDateFormat方法添加缺少的引号,或者我必须为此编写一个自定义的DateAdapter?从这个错误开始“java.text.ParseException:Unparseable date”通过rest by backbone save方法传递jsonObj“答案没有用,因为问题是关于用日期/时间字段绑定Json对象,而不仅仅是日期字段从dominik和我的例子开始,现在你可以知道不仅他的解决方案起作用,而且它的解决方案也有变化,因此,你可以使用你喜欢的格式,因为真正重要的是你必须在之前选择并设置它。知道我在jboss 7.x服务器上遇到的这个问题,而不是在6.x上遇到的这个问题可能会有用:序列化似乎在6.x上是不必要的
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response put(@HeaderParam
                    final String userFromString,
                    @Context final HttpServletRequest req) {

    JSONObject jsonObject = new JSONObject(userFromString);
    ReturnCode resultCode = null;
    UserDto user= new UserDto();
    Gson gson=  new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
    user= gson.fromJson(userFromString,UserDto.class);