[英]Convert UTC into Local Time on Android
在我的項目中,我得到了 json 格式的 API 響應。 我得到一個 UTC 時間格式的時間字符串值,例如Jul 16, 2013 12:08:59 AM
。
我需要將其更改為當地時間。 那就是我們使用這個應用程序需要顯示當地時間的地方。 我該怎么做?
這是我嘗試過的一些代碼:
String aDate = getValue("dateTime", aEventJson);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss z");
simpleDateFormat.setTimeZone(TimeZone.getDefault());
String formattedDate = simpleDateFormat.format(aDate);
假設 aDate 包含Jul 16, 2013 12:08:59 AM
這是我的嘗試:
String dateStr = "Jul 16, 2013 12:08:59 AM";
SimpleDateFormat df = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss a", Locale.ENGLISH);
df.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = df.parse(dateStr);
df.setTimeZone(TimeZone.getDefault());
String formattedDate = df.format(date);
還要注意上午/下午標記的“a”...
我想貢獻現代答案。 雖然SimpleDateFormat
是我們在 2013 年用於解析和格式化日期時間的類(除了 Joda-Time),但它現在已經過時了,而且我們在java.time
或 JSR-310(現代 Java 日期和2014 年隨 Java 8 發布的 time API。
但我聽到你說,大多數 Android 設備仍然不運行 Java 8。 幸運的是,您仍然可以通過 ThreeTenABP(JSR-310 到 Android Java 7 的反向移植)在它們上使用現代 Java 日期和時間 API。詳細信息在這個問題中:如何在 Android 項目中使用 ThreeTenABP 。
現在代碼是:
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("MMM dd, uuuu hh:mm:ss a", Locale.ENGLISH);
String aDate = "Jul 16, 2013 12:08:59 AM";
String formattedDate = LocalDateTime.parse(aDate, formatter)
.atOffset(ZoneOffset.UTC)
.atZoneSameInstant(ZoneId.systemDefault())
.format(formatter);
System.out.println(formattedDate);
由於我的計算機運行的是歐洲/哥本哈根時區,在 7 月比 UTC 早 2 小時,因此打印
Jul 16, 2013 02:08:59 AM
進一步的要點:
h
。 大寫H
表示從 0 到 23 的時間。SimpleDateFormat
還是DateTimeFormatter
)。 如果沒有給出語言環境,格式化程序將使用設備的默認語言環境。 “Jul”和“AM”是英文的,你的代碼可能在許多設備上運行良好,直到有一天它在非英語語言環境的設備上運行並崩潰,你很難弄清楚原因。ZoneId.of("Asia/Kolkata")
。 JVM 的默認時區可能會被您程序的其他部分或在同一 JVM 中運行的其他程序更改,因此不可靠。1.本地到UTC轉換器
public static String localToUTC(String dateFormat, String datesToConvert) {
String dateToReturn = datesToConvert;
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
sdf.setTimeZone(TimeZone.getDefault());
Date gmt = null;
SimpleDateFormat sdfOutPutToSend = new SimpleDateFormat(dateFormat);
sdfOutPutToSend.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
gmt = sdf.parse(datesToConvert);
dateToReturn = sdfOutPutToSend.format(gmt);
} catch (ParseException e) {
e.printStackTrace();
}
return dateToReturn;
}
2. UTC 到本地轉換器
public static String uTCToLocal(String dateFormatInPut, String dateFomratOutPut, String datesToConvert) {
String dateToReturn = datesToConvert;
SimpleDateFormat sdf = new SimpleDateFormat(dateFormatInPut);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date gmt = null;
SimpleDateFormat sdfOutPutToSend = new SimpleDateFormat(dateFomratOutPut);
sdfOutPutToSend.setTimeZone(TimeZone.getDefault());
try {
gmt = sdf.parse(datesToConvert);
dateToReturn = sdfOutPutToSend.format(gmt);
} catch (ParseException e) {
e.printStackTrace();
}
return dateToReturn; }
//your UTC time var
long time = UTCtime;
//convert it
Time timeFormat = new Time();
timeFormat.set(time+TimeZone.getDefault().getOffset(time));
//use the value
long localTime = timeFormat.toMillis(true);
使用以下代碼。
TimeZone defaultTimeZone = TimeZone.getDefault();
String strDefaultTimeZone = defaultTimeZone.getDisplayName(false, TimeZone.SHORT);
//The code you use
String aDate = getValue("dateTime", aEventJson);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss z");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone(strDefaultTimeZone));
String formattedDate = simpleDateFormat.format(aDate);
這應該有效。
這就是我在 android Build.VERSION.SDK_INT < 26 上的做法
int offset = TimeZone.getDefault().getRawOffset();
String str_date='20:30 12-01-2021';
DateFormat formatter = new SimpleDateFormat("HH:mm dd-MM-yyy",Locale.US);
Date date = formatter.parse(str_date);
long utcTime = date.getTime() + (3600000*3);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd/MM/yyyy", Locale.US);
String dateStr = sdf.format(utcTime + offset);
System.out.println(dateStr);
當我的服務器使用 -3 時區發送時間時,我必須將 (3600*3) 添加到 getTime 並將其保存到 utcTime,這樣 utcTime 就是 UTC。 然后我將手機當前時區的偏移量添加到 utcTime。 在我的情況下,因為我的時區是 -3 它的打印:
20:30 12/01/2021
但如果我改變我的時區,日期也會改變。
使用此代碼:
public static String stringDateWithTimezone(Date date, String pattern, TimeZone timeZone) {
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, Locale.US);
if (timeZone != null) {
simpleDateFormat.setTimeZone(timeZone);
}
return simpleDateFormat.format(date);
} catch (Exception e) {
Timber.e(e);
return null;
}
}
調用另一個類:
String dateUtc = DateUtil.stringDateWithTimezone(new Date(), "yyyy-MM-dd hh:mm:ss", TimeZone.getTimeZone("UTC"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.