我需要一个国际化字符串,它描述过去或未来Date对象与当前日期之间的比较。所以像..
我查看过具有getRelativeTimeSpanString()的DateUtils类,但这会以...等格式显示时间。
是否有任何人遇到的替代图书馆支持这种类型的东西,理想情况下也是国际化的。
答案 0 :(得分:2)
看看这段代码。这对你有帮助。
private String getPostDuration(String str_date) {
// TODO Auto-generated method stub
try {
//String str_date = "10-Aug-2011 21:20:00";
SimpleDateFormat formatter;
Date date;
//formatter = new SimpleDateFormat("dd/MMM/yyyy hh:mm:ss");
formatter = new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
date = (Date) formatter.parse(str_date);
//System.out.println("Today is " + date);
//System.out.println( date);
Date currentDate=new Date();
//System.out.println(currentDate);
long diff = currentDate.getTime() - date.getTime();
// System.out.println("The 21st century (up to " + today + ") is "
// + (diff / (1000 * 60 * 60 * 24)) + " days old.");
if ((diff / (1000 * 60 * 60 * 24)>0)) {
// System.out.println((diff / (1000 * 60 * 60 * 24)) + " days ago");
return (diff / (1000 * 60 * 60 * 24)) + " days old.";
}
else if((diff / (1000 * 60 * 60)>0)){
//System.out.println((diff / (1000 * 60 * 60)) + " hours ago");
return (diff / (1000 * 60 * 60 )) + " hours ago";
}
else if ((diff / (1000 * 60)>0))
{
//System.out.println((diff / (1000 * 60)) + " minutes ago");
return (diff / (1000 * 60 )) + " minutes ago";
}
else// if ((diff / (1000)>0))
{
//System.out.println((diff / (1000)) + "less than a minute ago");
return "less than a minute ago";
}
} catch (ParseException e) {
//System.out.println("Exception :" + e);
return "";
}
}