[英]Android how to get time difference between two time zones in android?
我需要獲取不同時區的兩個日期之間的時差。 目前我正在這樣做:
Calendar c1=Calendar.getInstance(TimeZone.getTimeZone("EDT"));
Calendar c2=Calendar.getInstance(TimeZone.getTimeZone("GMT"));
String diff=((c2.getTimeInMillis()-c1.getTimeInMillis())/(1000*60*60))+" hours";
new AlertDialog.Builder(this).setMessage(diff).create().show();
我得到0
小時。 我究竟做錯了什么?
getTimeInMillis()
返回自 UTC 紀元以來的毫秒數。 換句話說,時區與它無關。
我懷疑你實際上想要:
long currentTime = System.currentTimeMillis();
int edtOffset = TimeZone.getTimeZone("EDT").getOffset(currentTime);
int gmtOffset = TimeZone.getTimeZone("GMT").getOffset(currentTime);
int hourDifference = (gmtOffset - edtOffset) / (1000 * 60 * 60);
String diff = hourDifference + " hours";
喬恩很接近,但由於字符限制,我無法編輯他的答案。 這是相同的代碼,但東部標准時間的“EDT”更改為“EST”。
long currentTime = System.currentTimeMillis();
int edtOffset = TimeZone.getTimeZone("EST").getOffset(currentTime);
int gmtOffset = TimeZone.getTimeZone("GMT").getOffset(currentTime);
int hourDifference = (gmtOffset - edtOffset) / (1000 * 60 * 60);
String diff = hourDifference + " hours";
但是這個解決方案主要假設 TimeZone.getAvailableIDs() 在它的字符串數組中包含“EST”和“GMT”。 如果該方法不包含那些時區字符串,它將以 0 偏移量返回。
當時寫的問題和答案使用java.util
date-time API,這是 2011 年的正確做法。2014 年 3 月, 現代日期時間 API作為Java 8 標准庫的一部分發布,取代了遺留日期時間 API,從那時起強烈建議切換到java.time
,現代日期時間 API。
java.time
解決方案不要使用三字母時區 ID :在使用現代日期時間 API 之前,讓我們看看 Java 7 Timezone
文檔中的以下重要說明:
三字母時區 ID
為了與 JDK 1.1.x 兼容,還支持其他一些三字母時區 ID(例如“PST”、“CTT”、“AST”)。 但是,它們的使用已被棄用,因為相同的縮寫經常用於多個時區(例如,“CST”可能是美國的“Central Standard Time”和“China Standard Time”),Java 平台只能識別其中一個他們。
所需的解決方案:
import java.time.Instant;
import java.time.ZoneId;
import java.util.concurrent.TimeUnit;
class Main {
public static void main(String[] args) {
Instant now = Instant.now();
long offest1InSec = ZoneId.of("America/New_York").getRules().getOffset(now).getTotalSeconds();
long offest2InSec = ZoneId.of("Etc/UTC").getRules().getOffset(now).getTotalSeconds();
long hours = TimeUnit.HOURS.convert(offest2InSec - offest1InSec, TimeUnit.SECONDS);
System.out.println(hours);
}
}
Output 現在* :
5
上述方案是任意兩個時區的通用方案。 但是,如果所需的差異來自 GMT/UTC,它會變得更簡單。 在這種情況下,您不需要計算差異,因為時區的偏移量總是給出 w.r.t。 偏移量為00:00
的 UTC。
演示:
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.concurrent.TimeUnit;
class Main {
public static void main(String[] args) {
long hours = Math.abs(TimeUnit.HOURS.convert(
ZoneId.of("America/New_York").getRules().getOffset(Instant.now()).getTotalSeconds(), TimeUnit.SECONDS));
System.out.println(hours);
// Alternatively,
hours = Math.abs(TimeUnit.HOURS.convert(
ZonedDateTime.now(ZoneId.of("America/New_York")).getOffset().getTotalSeconds(), TimeUnit.SECONDS));
System.out.println(hours);
}
}
Output :
5
5
從Trail:Date Time了解有關現代日期時間 API 的更多信息。
* 美國東部時間(例如America/New_York )遵守夏令時。 在 DST 期間,它與 UTC 的偏移量是 4 小時,在非 DST 期間(例如現在,2023 年 1 月 28 日),它是 5 小時。 檢查此頁面以了解更多信息。
這是我的代碼二計算兩個不同時區之間的時間差。 像當前時區(GMT+05:00)和外國時區(GMT+05:30)。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LocalDate today = LocalDate.now();
Duration timeDifference = Duration.between(today.atStartOfDay(ZoneId.of(foreigntimezone)), today.atStartOfDay(ZoneId.of(currenttimezone)));
holder.textViewTimeDifference.setText(timeDifference.toString());
} else {
long now = System.currentTimeMillis();
long diffMilliSeconds = TimeZone.getTimeZone(gmtReplaceUTC).getOffset(now) - TimeZone.getTimeZone(currentTimeZone).getOffset(now);
if (diffMilliSeconds > 0) {
String timeDifference = formatTime(diffMilliSeconds);
holder.textViewTimeDifference.setText(timeDifference);
} else {
String timeDifference = formatTime(-diffMilliSeconds);
holder.textViewTimeDifference.setText("-" + timeDifference);
}
}
}
結果:對於 26 以上 API 級別 (PT+30M) 和以下 (30:00)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.