快速提问:有没有一种简单的方法可以将facebook API提供的原始时间转换为facebook应用程序上显示的时间格式?如果所有其他方法都失败了,可以编写一个新的算法,但我确定它已经存在(但显然不是在SO上 - 我搜索了它)。
Facebook应用程序上的时间如下: " 3秒前" " 5分钟前" " 19小时前" "昨天在等等等等#34; 等等。
谢谢!
答案 0 :(得分:1)
查看PrettyTime库。它非常易于使用。例如:
PrettyTime prettyTime = new PrettyTime(Locale.getDefault());
String theString = prettyTime.format(theDate);
答案 1 :(得分:0)
Facebook时代以三种不同的格式存储。尝试使用此方法将时间字符串数据转换为日历
public static Calendar convertToCalendar(String strDate) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf;
if (strDate.length() >= 22)
sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
else if (strDate.length() > 12)
sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
else {
sdf = new SimpleDateFormat("yyyy-MM-dd");
}
try {
sdf.parse(strDate);
cal.setTime(sdf.parse(strDate));
} catch (ParseException e) {
cal = null;
e.printStackTrace();
}
return cal;
}
您可以创建一个新的日历实例并执行计算以格式化您想要的时间文本。 我建议您使用Joda-Time Library来帮助您执行时间计算。您可以通过以下方式获得小时,分钟和秒的差异:
Hours.hoursBetween(cal1, cal2)
Minutes.minutesBetween(cal1, cal2)
Seconds.secondsBetween(cal1, cal2)