2016-03-01 113 views
1

這是我的代碼片段。我當地的時區是「亞洲/孟買」。時區變爲GMT,儘管我正在覆蓋時區

SimpleDateFormat isoFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm a"); 
    Date date1 = isoFormat.parse("03/01/2016 09:01 AM"); 
    isoFormat.setTimeZone(TimeZone.getTimeZone("Asia/Mumbai")); 

    isoFormat.applyPattern("dd MMM yyyy HH:mm:ss z"); 
    System.out.println("Current Date and Time in IST time zone: " + isoFormat.format(date1)); 

但在輸出我得到:

目前在IST時區的日期和時間:2016年3月1日3時31分00秒GMT

誰能告訴爲什麼重寫一個時區是我的本地時區也可以將其更改爲GMT嗎?

儘管JST或SGT的代碼工作正常,

isoFormat.setTimeZone(TimeZone.getTimeZone("Asia/Singapore")); 
    isoFormat.applyPattern("dd MMM yyyy HH:mm:ss z"); 
    System.out.println("Current Date and Time in SGT time zone: " + isoFormat.format(date1)); 

    isoFormat.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo")); 
    isoFormat.applyPattern("dd MMM yyyy HH:mm:ss z"); 
    System.out.println("Current Date and Time in JST time zone: " + isoFormat.format(date1)); 

輸出:

當前日期和時間在SGT時區:2016年3月1日11時31分00秒SGT

當前日期和時間JST時區:2016年3月1日12:31 :00 JST

回答

0

嘗試用以下代碼:

isoFormat.setTimeZone(TimeZone.getTimeZone( 「亞/加爾各答」));