在我的Android应用程序服务器将返回一些UTC
日期格式为(yyyy-MM-dd HH:mm:ss
)24小时,我需要将这些时间转换为用户的TimeZone for示例CST
,IST
。如何将UTC时间转换为其他时区(“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
TimeZone.getDefault()。getOffset(now.getTime()),现在是用户的当前时间,应该给GMT设备的时区偏移量。 – mraviator