2013-03-18 21 views
0

我在IST時間在Java中有一個Date字段。我想將其轉換爲EST時間,並且輸出應僅作爲日期類型。我可以使用下面的代碼來完成相同的: -IST到EST在Java中的時間轉換

SimpleDateFormat dateTimeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 
dateTimeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta")); 
Date date = new Date(); 
DateFormat timeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 
timeFormat.setTimeZone(TimeZone.getTimeZone("America/New_York")); 
String estTime = timeFormat.format(date); 
date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.ENGLISH).parse(estTime); 

與上面這段代碼的問題是,雖然日期是美國東部時間時間轉換,時區仍然顯示爲IST和而不是EST。日期的其餘部分轉換得很好。有沒有什麼辦法可以明確地設置日期字段中的時區到EST。任何有關這方面的幫助將不勝感激。

-Subhadeep

+0

'dateTimeFormat'是在代碼中未使用的變量。 – 2013-03-18 12:42:53

回答

1

Date類是時區無關。基本上,它始終基於GMT,但打印時使用當前系統時區進行調整。

但是,Calendar是時區特定的。見Calendar.setTimeZone()

考慮:

Calendar cal = new GregorianCalendar(); 
    cal.setTimeZone(TimeZone.getTimeZone("America/New_York")); 
    cal.setTime(new Date()); 
0

如果您在SimpleDateFormat模式添加z的時區模式?

所以,它應該是DateFormat timeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z")。我改變了你這樣的代碼:

public static void main(String[] args) throws ParseException { 
    SimpleDateFormat dateTimeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z"); 
    dateTimeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta")); 
    Date date = new Date(); 

    System.out.println(dateTimeFormat.format(date)); // this print IST Timezone 

    DateFormat timeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z"); 
    timeFormat.setTimeZone(TimeZone.getTimeZone("America/New_York")); 

    String estTime = timeFormat.format(date); 
    date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z", Locale.ENGLISH).parse(estTime); 

    System.out.println(timeFormat.format(date)); // this print EDT Timezone currently (on March) 
} 

在過去的print語句,當前的日期格式打印EDT時區(東部夏令時間)。也許是因爲this

+0

不能使用Z參數。日期的其餘部分現在也在IST中:( – user2182350 2013-03-18 13:24:38

+0

即使小寫('z')?這是因爲大寫字母('Z')只是打印相對GMT時間的相對時間,例如'-0400'。 – 2013-03-18 13:31:45

+0

嘗試過與小z只...在第一個回覆中的錯字:( – user2182350 2013-03-18 13:38:15

0

正確的answer by John B解釋說java.util.Date似乎有一個時區,但沒有。它的toString方法在生成字符串表示時應用您的JVM的默認時區。

這是避免與Java捆綁在一起的java.util.Date和.Calendar類的很多原因之一。避免它們。取而代之的是使用Joda-Time或Java 8中內置的java.time包(受Joda-Time的啓發,由JSR 310定義)。

以下是Joda-Time 2.3中的一些示例代碼。

String input = "01/02/2014 12:34:56"; 
DateTimeFormatter formatterInput = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss"); 
DateTimeZone timeZoneIndia = DateTimeZone.forID("Asia/Kolkata"); 
DateTime dateTimeIndia = formatterInput.withZone(timeZoneIndia).parseDateTime(input); 

DateTimeZone timeZoneNewYork = DateTimeZone.forID("America/New_York"); 
DateTime dateTimeNewYork = dateTimeIndia.withZone(timeZoneNewYork); 

DateTime dateTimeUtc = dateTimeIndia.withZone(DateTimeZone.UTC); 

轉儲到控制檯...

System.out.println("input: " + input); 
System.out.println("dateTimeIndia: " + dateTimeIndia); 
System.out.println("dateTimeNewYork: " + dateTimeNewYork); 
System.out.println("dateTimeUtc: " + dateTimeUtc); 

當運行...

input: 01/02/2014 12:34:56 
dateTimeIndia: 2014-01-02T12:34:56.000+05:30 
dateTimeNewYork: 2014-01-02T02:04:56.000-05:00 
dateTimeUtc: 2014-01-02T07:04:56.000Z 
相關問題