2014-07-02 78 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

相關問題