2011-12-13 104 views
1

因此,我將時間戳保存爲Date對象,將時區保存爲TimeZone對象。如何將java日期轉換爲基於時區的日期對象?

現在我想做一個函數,它將一個對象012和一個對象TimeZone作爲參數,並返回一個使用時間戳調整的對象Date

例如:

輸入:

 
Date TimeZone 

12:00 Moscow Standard Time (UTC+3) 

輸出:

 
Date 

3:00  

編輯: 刪除備註Calendar

+0

日曆是做到這一點的方式。你的用例與你用來解決問題的內部對象有什麼關係? – Thom 2011-12-13 21:07:41

+0

我的意思是我不想保存日曆對象的分貝,虐待編輯,澄清。你可以提供如何使用日曆轉換它的例子嗎? – Jimmy 2011-12-13 21:10:19

+0

ahh joda time讓生活變得如此簡單。 – Andy 2011-12-13 21:13:28

回答

7

A java.util.Date是絕對時間點。 09:00小時UTC1200小時UTC + 3完全相同java.util.Date對象。爲了代表這個或那個,沒有對它進行「調整」。

要獲取可讀的表示形式來說明特定的時區,可以在DateFormat對象上設置時區。

DateFormat format = new SimpleDateFormat("HH:mm"); 
format.setTimeZone(TimeZone.getTimeZone("UTC+3")); 
String time = format.format(yourDate); 

解決方案在評論中提出的問題:

Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("UTC+3")); 
cal1.setTime(yourDate); 
Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("UTC")); 
cal2.clear(); 
cal2.set(Calendar.YEAR, cal1.get(Calendar.YEAR)); 
cal2.set(Calendar.MONTH, cal1.get(Calendar.MONTH)); 
cal2.set(Calendar.DATE, cal1.get(Calendar.DATE)); 
cal2.set(Calendar.HOUR_OF_DAY, cal1.get(Calendar.HOUR_OF_DAY)); 
//simile for whatever level of field precision is needed 
Date shiftedDate = cal2.getTime(); 
0

如果你沒有用C alendar,你在用什麼?另一個圖書館呢?

如果沒有,那麼你的時區末尾有那個+3 ...你可以用它來衝擊小時(+/-)X;在這種情況下,+3。請記住,在這種情況下(例如)11 + 3 = 2。這個數學可以通過添加小時+偏移量,取值12,並將該答案設置爲小時(如果需要,設置0到12的回答)來完成。那有意義嗎?

0

我發現了一個簡單的方法來完成,使用所需的時區的rowOffset:

Date date = new Date(); 
int rawOffset = TimeZone.getTimeZone("EST").getRawOffset(); 
Date adjustedDate = new Date(date.getTime() + rawOffset) 

編輯:如情緒指出,這將忽略閏秒和夏令時。

1

在這裏你去:

/** 
* Convert a calendar from its current time zone to UTC (Greenwich Mean Time) 
* @param local the time 
* @return a calendar with the UTC time 
*/ 
public static Calendar convertTimeToUtc(Calendar local){ 
    int offset = local.getTimeZone().getOffset(local.getTimeInMillis()); 
    GregorianCalendar utc = new GregorianCalendar(TZ_UTC); 
    utc.setTimeInMillis(local.getTimeInMillis()); 
    utc.add(Calendar.MILLISECOND, -offset); 

    return utc; 
} 

/** 
* Convert a UTC date into the specified time zone 
* @param tzName the name of the time zone for the output calendar 
* @param utc the UTC time being converted 
* @return a calendar in the specified time zone with the appropriate date 
*/ 
public static Calendar convertTimeToLocal(String tzName, Calendar utc) { 
    TimeZone zone = TimeZone.getTimeZone(tzName); 
    int offset = zone.getOffset(utc.getTimeInMillis()); 
    GregorianCalendar local = new GregorianCalendar(zone); 
    local.setTimeInMillis(utc.getTimeInMillis()); 
    local.add(Calendar.MILLISECOND, offset); 

    return local; 
} 

/** 
* Convert a UTC date into the specified time zone 
* @param zone the time zone of the output calendar 
* @param utc the UTC time being converted 
* @return a calendar in the specified time zone with the appropriate date 
*/ 
public static Calendar convertTimeToLocal(TimeZone zone, Calendar utc) { 
    int offset = zone.getOffset(utc.getTimeInMillis()); 
    GregorianCalendar local = new GregorianCalendar(zone); 
    local.setTimeInMillis(utc.getTimeInMillis()); 
    local.add(Calendar.MILLISECOND, offset); 

    return local; 
} 
相關問題