我的安卓应用程序会从服务器返回一些UTC日期,格式为(yyyy-MM-dd HH:mm:ss
) 24小时制,我需要将这些时间转换为用户所在的时区,例如CST
,IST
。
我尝试了以下代码,但我不确定它是否正确,请帮助我以正确的方式进行时区转换。
我获取UTC日期作为json字符串,并将其转换为用户的时区格式,并在Android端显示
private static Date utcDate;
private static DateFormat expireFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
try {
expireFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
utcDate = expireFormat.parse("2014-04-01 10:32:00");
System.out.println(TimeZone.getDefault().getID());
expireFormat.setTimeZone(TimeZone.getTimeZone(TimeZone.getDefault().getID()));
System.out.println(expireFormat.format(utcDate));
} catch (ParseException e) {
e.printStackTrace();
}
output of the code:
Asia/Calcutta
2014-04-01 16:02:00