相关疑难解决方法(0)

使用gson将Java日期转换为UTC

我似乎无法让gson在Java中将Date转换为UTC时间....这是我的代码......

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create();
//This is the format I want, which according to the ISO8601 standard - Z specifies UTC - 'Zulu' time

Date now=new Date();          
System.out.println(now);       
System.out.println(now.getTimezoneOffset());
System.out.println(gson.toJson(now));
Run Code Online (Sandbox Code Playgroud)

这是我的输出

Thu Sep 25 18:21:42 BST 2014           // Time now - in British Summer Time 
-60                                    // As expected : offset is 1hour from UTC    
"2014-09-25T18:21:42.026Z"             // Uhhhh this is not UTC ??? Its still BST !!
Run Code Online (Sandbox Code Playgroud)

我想要的gson结果和我期待的结果

"2014-09-25T17:21:42.026Z"
Run Code Online (Sandbox Code Playgroud)

我可以清楚地在调用Json之前减去1小时,但这似乎是一个黑客.如何配置gson始终转换为UTC?

java json date utc gson

35
推荐指数
1
解决办法
3万
查看次数

标签 统计

date ×1

gson ×1

java ×1

json ×1

utc ×1