【发布时间】:2016-09-15 07:23:47
【问题描述】:
我使用以下代码将 UTC 时间转换为设备当前时区时间。它在印度标准时间 (IST) 上运行良好。但是当设备时区是澳大利亚悉尼时,它会额外增加 1 小时。
例如,我给 UTC 时间,例如 2016-10-10T09:10:00,它转换为晚上 8:10,但实际上我需要晚上 7:10。我的代码有什么问题?
public static String convertToLocalTimeFormat(String utcDateTimeString) {
SimpleDateFormat simpleDateFormat;
String formattedDateString = "";
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date parsedDateObject;
try {
parsedDateObject = simpleDateFormat.parse(utcDateTimeString);
SimpleDateFormat expectedSimpleDateFormat = new SimpleDateFormat("hh:mm a", Locale.getDefault());
formattedDateString = expectedSimpleDateFormat.format(parsedDateObject);
} catch (ParseException e) {
e.printStackTrace();
return formattedDateString;
}
return formattedDateString;
}
【问题讨论】:
-
夏令时?
-
@Tibrogargan 是的...目前在澳大利亚是澳大利亚东部标准时间,但是给定的日期是 10 月...所以在 10 月 1 日之后是澳大利亚东部夏令时间。所以它增加了1小时。但我需要同时显示我现在输入的内容。
标签: java android android-date