【发布时间】:2019-03-14 00:02:36
【问题描述】:
我已完成以下代码将时间从 UTC 更改为另一个时区,但代码仅显示 UTC 时间。此外,在格式化为源时间格式后,它显示系统时区。
private String setTimezone(String time){
sourceformatter = new SimpleDateFormat("hh:mm a, E dd MMM yyyy");
dateFormatter = new SimpleDateFormat("hh:mm a");
sourceformatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Log.e("reicievedformat",time);
Date value = null;
try {
value = sourceformatter.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
Log.d("afterfirstformat",dateFormatter.format(value));
dateFormatter.setTimeZone(TimeZone.getTimeZone("IST"));
time =dateFormatter.format(value);
Log.d("Finaltime",time);
return time;
}
输出:- 记录值
电子/接收格式:2018 年 10 月 8 日星期一下午 12:36
D/afterfirstformat:下午 6:21
晚/决赛时间:下午 12:36
如您所见,我将在 2018 年 10 月 8 日星期一(“UTC”)下午 12:36,我想转换为 IST,但最后一次 12:36 PM 似乎尚未转换.
【问题讨论】:
-
嗨,欢迎来到堆栈溢出。现在你的要求是什么?能详细点吗?
-
你期望的输出是什么
-
2018 年 10 月 8 日星期一(“UTC”)下午 12:36,我想转换为 IST
-
顺便考虑扔掉长期过时且臭名昭著的麻烦
SimpleDateFormat和朋友,并将ThreeTenABP添加到您的Android项目中,以便使用java.time,现代Java日期和时间API .与它一起工作要好得多。LocalDateTime.parse("12:36 PM, Mon 08 Oct 2018", DateTimeFormatter.ofPattern("hh:mm a, E dd MMM yyyy", Locale.ENGLISH)).atOffset(ZoneOffset.UTC).atZoneSameInstant(ZoneId.of("Asia/Kolkata")).format(DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH)).
标签: java android timezone simpledateformat datetime-conversion