2014-07-02 150 views
2

我有一个显示Mountain时区的字符串“2014-07-02T17:12:36.488-01:00”。我将其解析为java.util.date格式。现在我需要将其转换为GMT格式。谁能帮我??如何将java.util.Date转换为GMT格式

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); 
    Object dd = null; 
    try { 
     dd=sdf.parseObject("2014-07-02T17:12:36.488-01:00"); 
     System.out.println(dd); 
    } catch (ParseException e) { 
     e.printStackTrace();`enter code here` 
    } 
    SimpleDateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    gmtDateFormat.setTimeZone(java.util.TimeZone.getTimeZone("GMT")); 
System.out.println("Current Date and Time in GMT time zone:+ gmtDateFormat.format(dd)); 
+0

[转换符合ISO8601字符串到java.util.Date](可能重复http://stackoverflow.com/questions/2201925/converting-iso8601-compliant-string-to-java- util-date) –

回答

3

你的代码有几个问题。例如,格式字符串与您正在解析的字符串的实际格式不匹配。

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX"); 
Object dd = null; 
try { 
    dd = sdf.parse("2014-07-02T17:12:36.488-01:00"); 
    System.out.println(dd); 
} catch (ParseException e) { 
    e.printStackTrace(); 
} 

SimpleDateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssX"); 
gmtDateFormat.setTimeZone(java.util.TimeZone.getTimeZone("GMT")); 

System.out.println("Current Date and Time in GMT time zone:" + gmtDateFormat.format(dd)); 

要打印你喜欢的任何时区的当前日期,设置要在SimpleDateFormat物体上使用的时区。例如:

// Create a Date object set to the current date and time 
Date now = new Date(); 

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX"); 
df.setTimeZone(TimeZone.getTimeZone("GMT")); 
System.out.println("Current date and time in GMT: " + df.format(now)); 

df.setTimeZone(TimeZone.getTimeZone("IST")); 
System.out.println("Current date and time in IST: " + df.format(now)); 
+0

感谢您的支持 – user3798050

+0

Wed Jul 02 23:42:36 IST 2014 当前日期和时间格林威治标准时间时区:2014-07-02 18:12:36Z ..我有这样的输出。什么是Z在(2014-07-02 18:12:36Z)。 – user3798050

+0

在同一时间IST MST时差是11.30 bt这里是12.30小时。我能做些什么来获得准确的值? – user3798050

相关问题