2011-12-30 102 views
0
Calendar cal = Calendar.getInstance(); 
cal.setTimeZone(TimeZone.getTimeZone("PST")); 
cal.setTime(new Date()); 

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a"); 

Date resultdate = new Date(cal.getTimeInMillis()); 
sdf.setTimeZone(TimeZone.getTimeZone("PST")); 

System.out.println("String date:"+sdf.format(resultdate)); 
System.out.println("Date:"+sdf.parse(sdf.format(resultdate))); 

輸出:如何日期和時間轉換爲用戶時區

String date:2011-12-29 09:01:58 PM            
Date:Fri Dec 30 10:31:58 IST 2011 

問題:

  1. sdf.format(resultdate)返回正確的日期和時間爲每個時區。但是,
  2. sdf.parse(sdf.format(resultdate))不按照時區返回正確的日期和時間,如何解決這個問題?
+0

你可以只使用[喬達時間](http://joda-time.sourceforge.net/)? – 2011-12-30 05:13:00

回答

0

Unfortunatley Java日期以GMT格式返回時間。如果您想要在前端或某處顯示,則需要使用step1中生成的格式化字符串。

2

Date這個類僅僅是圍繞「時代」(1970年1月1日,格林威治標準時間00:00:00)毫秒數的薄包裝。它不存儲任何時區信息。在上次調用中,您將一個日期實例添加到String,這隱含地調用toString()方法。 toString()方法將使用默認時區來創建代表實例的String(因爲它不存儲任何時區信息)。嘗試修改最後一行以避免使用toString()方法。

System.out.println("Date:" + sdf.format(sdf.parse(sdf.format(resultdate)))); 
0

請嘗試下面的代碼,它會工作。

Calendar cal = Calendar.getInstance(); 
cal.setTimeZone(TimeZone.getTimeZone("PST")); 
cal.setTime(new Date()); 

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a"); 
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a"); 

Date resultdate = new Date(cal.getTimeInMillis()); 
sdf.setTimeZone(TimeZone.getTimeZone("PST")); 

System.out.println("String date:"+sdf.format(resultdate)); 
System.out.println("Date:"+sdf2.parse(sdf.format(resultdate))); 
0

三字母時區代碼

避免使用三字母時區代碼。它們既不標準也不唯一。例如,IST表示愛爾蘭標準時間印度標準時間。此外,這些代碼旨在區分夏令時(DST),但這隻會讓事情混淆不清。

使用正確的descriptive time zone names來檢索包含DST和其他問題的時區對象。

喬達時間

與Java捆綁與java.util.Date & Calendar類是出了名的麻煩。避免它們。使用Joda-Time或與Java 8捆綁的新java.time。*包。

在JodaTime中,DateTime對象真正知道它自己的時區(與java.util.Date不同)。通常我們使用Joda-Time中的不可變類。因此,我們不是在DateTime對象中更改時區,而是基於舊的但具有指定的差異的DateTime對象創建新的新的對象。不同的時區可能就是這種差異。

以下是一些示例代碼。

DateTimeZone timeZone_India = DateTimeZone.forID("Asia/Kolkata"); 
DateTimeZone timeZone_Ireland = DateTimeZone.forID("Europe/Dublin"); 
DateTimeZone timeZone_US_West_Coast = DateTimeZone.forID("America/Los_Angeles"); 

DateTime now = new DateTime(timeZone_India); 

System.out.println("now in India: " + now); 
System.out.println("now in Ireland: " + now.withZone(timeZone_Ireland)); 
System.out.println("now in US West Coast: " + now.withZone(timeZone_US_West_Coast)); 
System.out.println("now in UTC/GMT: " + now.withZone(DateTimeZone.UTC)); 

當運行...

now in India: 2014-02-10T13:52:27.875+05:30 
now in Ireland: 2014-02-10T08:22:27.875Z 
now in US West Coast: 2014-02-10T00:22:27.875-08:00 
now in UTC/GMT: 2014-02-10T08:22:27.875Z 

的Java。時間

使用取代Joda-Time的java.time類的相同想法。

Instant類代表UTC中的時間軸上的一個時刻,分辨率爲nanoseconds(最多九(9)位小數)。

Instant instant = Instant.now(); 

應用時區。

ZoneId z = ZoneId.of("America/Montreal"); 
ZonedDateTime zdt = instant.atZone(z); 

instantzdt代表同一時刻,在時間軸上的相同點。通過不同區域的鏡頭wall-clock time可以看到每個鏡頭。

通過指定格式化模式或讓java.time自動進行本地化來生成字符串。

要本地化,指定:

  • FormatStyle確定字符串應該多長或縮寫是。
  • Locale確定(a)翻譯日期名稱,月份名稱等的人類語言,以及(b)確定縮寫,大寫,標點等問題的文化規範。

例子:

Locale l = Locale.CANADA_FRENCH ; 
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withLocale(l); 
String output = zdt.format(f); 
相關問題