码:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(new Date());
try {
String d = sdf.format(new Date());
System.out.println(d);
System.out.println(sdf.parse(d));
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
Run Code Online (Sandbox Code Playgroud)
输出:
Thu Aug 08 17:26:32 GMT+08:00 2013
2013.08.08 09:26:32 GMT
Thu Aug 08 17:26:32 GMT+08:00 2013
Run Code Online (Sandbox Code Playgroud)
请注意,format()格式Date正确格式为GMT,但parse()丢失了GMT详细信息.我知道我可以使用substring()并解决这个问题,但这种现象背后的原因是什么?
这是一个重复的问题,没有任何答案.
编辑:让我以另一种方式提出问题,检索Date对象的方式是什么,以便它始终在GMT中?