Java Date to UTC using gson
我似乎无法让gson在java中将日期转换为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));
这是我的输出
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 !!
我想要的gson结果和我期待的
"2014-09-25T17:21:42.026Z"
我显然可以在打电话给Json之前减去1小时,但这似乎是一个黑客。如何配置gson始终转换为UTC?