我正在将UTC时间转换为另一个时区,使用以下方法:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parsed = format.parse("2011-03-01 15:10:37");
TimeZone tz = TimeZone.getTimeZone("America/Chicago");
format.setTimeZone(tz);
String result = format.format(parsed);
输入是2011-03-01 15:10:37
,但这个函数的输出结果(即result
的值)是2011-03-01 05:40:37
。虽然看起来不对劲,但根据此链接,答案应该是2011-03-01 09:10:37
。
我做错了什么?
sourceFormat.parse()
周围加上try{} catch (ParseException e){}
。此外,为了获取手机所在地区的本地时区,请使用TimeZone.getDefault()
,以便将UTC转换为本地时区。 - Someone Somewhereformater.setTimeZone(TimeZone.getTimeZone(RBApplication.adjustTimezoneAEST));
即可。谢谢,我已经为这个问题奋斗了几个小时! - wired00