我正在尝试使用以下代码将我的机器的日期/时间转换为 GMT 时区:
DateFormat gmtFormat = new SimpleDateFormat("E MMM dd HH:mm:ss 'GMT' yyyy");
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
gmtFormat.setTimeZone(gmtTime);
System.out.println("Orginal Date : " + new Date());
String s= gmtFormat.format(new Date());
System.out.println("Converted Date As String: "+ s);
System.out.println("Converted Date As date object : " + gmtFormat.parse(s));
以上代码输出:
Orginal Date : Mon Apr 21 21:04:06 AST 2014
Converted Date As String: Mon Apr 21 18:04:06 GMT 2014
Converted Date As date object : Mon Apr 21 21:04:06 AST 2014
但是,当我使用 parse 函数将“Converted Date As String”解析为“Converted Date As date object”时出现问题,但您可以注意到日期更改为原始日期
为什么会这样?
当我执行以下操作时,问题就解决了:
DateFormat gmtFormat = new SimpleDateFormat("E MMM dd HH:mm:ss 'GMT' yyyy");
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
gmtFormat.setTimeZone(gmtTime);
System.out.println("Orginal Date : " + new Date());
String s= gmtFormat.format(new Date());
System.out.println("Converted Date As String: "+ s);
// Just I added Local Time Zone Formatand I use it to call parse instead of gmtFormat
DateFormat LocalFormat = new SimpleDateFormat("E MMM dd HH:mm:ss 'GMT' yyyy");
System.out.println("Converted Date As date object : " + LocalFormat.parse(s));
我想知道为什么会这样 >> Local format 如何与 parse Function 相关?有谁知道为什么?