隐藏UTC时间格式到当地时间
问题描述:
我有这种格式的UTC时间2013-03-04T14:37:15
如何将此转换为当地时间,然后再转换为one minute ago
格式。隐藏UTC时间格式到当地时间
我想这首先将时间转换为本地格式:
private String getDate(String date) {
Date fDate = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
fDate = simpleDateFormat.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fDate.toString();
}
但上述功能抛出一个异常,说无法解析的日期..
答
你得到的解析异常是由于额外T
在你的日期(2013-03-04T14:37:15
)。
您可以删除多余的“T”,并与您的当前日期格式解析日期,
或者,如果你需要用T
解析它,改变你的格式解析字面T
为好,
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+1
工作正常现在我得到格式化的日期是这样的:Mon Mar 04 20:07: 15 GMT + 05:30 2013,如何将其更改为所需的格式,例如:yyyy-mm-dd 04:30 PM – sukarno 2013-03-19 17:10:19
答
检查DateUtils.getRelativeTimeSpanString()(一个与DateUtils同名的方法)。这将是这样的:
Date fDate = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
fDate = simpleDateFormat.parse(date);
return DateUtils.getRelativeTimeSpanString(context, System.currentTimeMillis()-fDate.getTime())'
类似[转换UTC到当前区域设置的时间(http://stackoverflow.com/questions/6049207/convert-utc-to-current-locale-time)和[转换UTC日期到其他时区](http://stackoverflow.com/questions/6088778/converting-utc-dates-to-other-timezones) – McArthey 2013-03-19 16:40:14