2011-10-07 66 views
12

我有時間以毫秒爲單位,我在"MM/dd/yyyy HH:mm:ss"格式的應用程序中顯示那個時間,它工作正常。Android中的時區問題

問題:如果我在Android設備中更改時區,它會在我的應用程序中顯示不同的時間,可能是因爲某些時區計算。我想顯示在我的數據庫中的相同時間,因爲它在任何時區。

我使用下面的代碼進行測試時區問題

Locale locale = new Locale("en", "IN"); 

TimeZone tz = TimeZone.getDefault(); 
System.out.println("Default timezon id :: " + tz.getID()); 
tz.setID("Asia/Calcutta"); 
String timezon = tz.getDisplayName(false, TimeZone.SHORT, locale); 
System.out.println("Timezon id :: " + tz.getID() + " Timezon name :: " + timezon); 
Calendar cal = Calendar.getInstance(tz, locale); 

System.out.println("TImezon :: " + tz); 
// "1319084400775" is the milliseconds of 10/20/2011 05:20:00 in MM/dd/yyyy HH:mm:ss format 
Date date = new Date(Long.parseLong("1319084400775")); 
System.out.println("Date :: " + date.toGMTString()); 
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", locale); 

System.out.println("Start date :: " + format.format(date)); 
// "1319084400775" is the milliseconds of 10/20/2011 05:20:00 in MM/dd/yyyy HH:mm:ss format 
cal.setTimeInMillis(Long.parseLong("1319084400775")); 

timezon = cal.getTimeZone().getDisplayName(false, TimeZone.SHORT, locale); 
System.out.println("Calender's timezon :: " + timezon); 
System.out.println("Start date :: yyyy-mm-dd hh:mm:ss " + cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DAY_OF_MONTH) + " " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE)); 
// "1319084700776" is the milliseconds of 10/20/2011 05:25:00 in MM/dd/yyyy HH:mm:ss format 
cal.setTimeInMillis(Long.parseLong("1319084700776")); 
System.out.println("End date :: " + cal.getTime()); 

輸出::

如果設置時區爲 「太平洋/斐濟」;

10-07 17:03:40.392: INFO/System.out(1193): Default timezon id :: Pacific/Fiji 
10-07 17:03:40.392: INFO/System.out(1193): Timezon id :: Asia/Calcutta Timezon name :: GMT+05:30 
10-07 17:03:40.406: INFO/System.out(1193): TImezon :: [email protected] 
10-07 17:03:40.422: INFO/System.out(1193): Date :: 20 Oct 2011 04:20:00 GMT 
10-07 17:03:40.442: INFO/System.out(1193): Start date :: 2011-10-20 16:20:00 
10-07 17:03:40.442: INFO/System.out(1193): Calender's timezon :: GMT+05:30 
10-07 17:03:40.442: INFO/System.out(1193): Start date :: yyyy-mm-dd hh:mm:ss 2011-9-20 16:20 
10-07 17:03:40.452: INFO/System.out(1193): End date :: Thu Oct 20 16:25:00 Pacific/Fiji 2011 

如果設置時區爲 「美洲/蒂華納」;

10-06 22:05:20.702: INFO/System.out(1193): Default timezon id :: America/Tijuana 
10-06 22:05:20.712: INFO/System.out(1193): Timezon id :: Asia/Calcutta Timezon name :: GMT+05:30 
10-06 22:05:20.712: INFO/System.out(1193): TImezon :: [email protected] 
10-06 22:05:20.733: INFO/System.out(1193): Date :: 20 Oct 2011 04:20:00 GMT 
10-06 22:05:20.742: INFO/System.out(1193): Start date :: 2011-10-19 21:20:00 
10-06 22:05:20.742: INFO/System.out(1193): Calender's timezon :: GMT+05:30 
10-06 22:05:20.752: INFO/System.out(1193): Start date :: yyyy-mm-dd hh:mm:ss 2011-9-19 21:20 
10-06 22:05:20.752: INFO/System.out(1193): End date :: Wed Oct 19 21:25:00 America/Tijuana 2011 

編輯

我在毫秒的時間我現在顯示的是毫秒的日期格式在我的應用程序,但問題是,當我在設備改變時區我按照該時區獲取日期。

+0

你可以在打印行中添加(cal.get(Calendar.MONTH)+1)嗎?並仍然困惑你的查詢是關於什麼? – jazz

+0

@Shahzeb這是什麼? – Dharmendra

+0

@jazz你可以看到輸出,當我改變timezon輸出是不同的,我不想意味着我想要我輸入相同的日期和時間 – Dharmendra

回答

12

日期只是世界時的瞬間。它沒有時區的概念。把它看作是「肯尼迪遇難的時候」。

當您使用SimpleDateFormat顯示此時間時,此日期格式有一個時區,並且您使用此時區即時顯示此時間。所以,如果日期格式的時區是中央標準時間,則此時間將顯示爲12:30。如果時區是UTC,則它將顯示爲18:30。

如果您希望以相同的方式顯示某個日期,而不管時區如何,您只需選擇特定時區(例如UTC),並始終顯示該時區的日期。因此,在格式化日期之前,您必須在DateFormat上撥打setTimeZone

+0

但是如果我想使用Calendar,那麼我該如何解決這個問題?請幫助我 – Dharmendra

+2

日曆是一個約會的包裝。調用getTime(),你會得到一個日期。 –

+0

非常感謝很多人我得到的解決方案 – Dharmendra

4

在標準的Java:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
sdf.setTimeZone(TimeZone.getTimeZone(<your timezone here>); 

long millis = <your timestamp here>; //timestamps are not TZ dependent. 
Date d = new Date(millis); 
String toDisplay = sdf.format(d); 

的時區在SimpleDateFormat是您要使用的每一個顯示日期,而不是設備的默認一個固定的時區。 還要確保數據庫中的timestamp列在相同的時間點開始計時,並且具有相同的精度(毫秒),這個時間點比Java(1970年1月1日,AKA Unix epoch)時間更長。

還有另一個android.text.format.DateFormat類,但我沒有看到使用它的任何一點。

+0

SimpleDateFormat使用timezone.I檢查你的方式,但沒有解決我的問題 – Dharmendra

+1

它必須工作。你必須設置一個固定的時區(總是相同的)。例如,要在印度顯示日期,請使用印度TZ。當設備TZ改變時,輸出應該是相同的。 –

+0

是的,它正在工作,但如果我想使用日曆,那麼我怎樣才能解決這個問題 – Dharmendra

3

我得到的解決方案類似下面的方法

/** 
    * Date time must be in milliseconds 
    * return Time in milliseconds 
    */ 
    public static String getTimeInMilli(Context ctx, String datetime) { 
     Calendar cal = Calendar.getInstance(); 
     cal.setTimeInMillis(Long.parseLong(datetime)); 
     //This will return daylight savings offset in milliseconds.You have to Subtract this milliseconds from calendar 
     int dayLightSaving = cal.get(Calendar.DST_OFFSET); 

     cal.setTimeInMillis(cal.getTimeInMillis() - dayLightSaving); 

     TimeZone z = cal.getTimeZone(); 
     int offset = z.getRawOffset(); 
     int offsetHrs = offset/1000/60/60; 
     int offsetMins = offset/1000/60 % 60; 

     // Subtract offset of your current TimeZone 
     cal.add(Calendar.HOUR_OF_DAY, (-offsetHrs)); 
     cal.add(Calendar.MINUTE, (-offsetMins)); 

     cal.setTimeZone(TimeZone.getTimeZone("GMT")); 

     //add offset of your TimeZone 
     cal.add(Calendar.HOUR_OF_DAY, 5); 
     cal.add(Calendar.MINUTE, 30); 

     return String.valueOf(cal.getTimeInMillis()); 
    } 

現在,如果我更改時區在我的設備我的應用程序的日期將保持不變。