我必须显示发布消息后经过的时间。我正在编写一个方法,将publisheddate
其作为输入并返回经过的时间。
d1.gettime-pubdate.gettime=time in milliseconds.
我正在写一个 if 条件
if time==something concat days ago,
elseif hours ago
elseif min ago
elseif seconds ago
elseif justnow.
if 条件应该包含什么?例如天数应该是if(time>=1440)
(如果我以分钟为单位计算日期,这可以通过 来完成time/1000*60*60
);我应该在几分钟、几小时等时间里做什么?
根据这里给出的建议是我写的。任何改进建议?
public static String TimeElapsed(final Date date)
{
Date Interval=new Date();
DateTime Interval1 = new DateTime();//Converting java time to joda time
Interval1 = new DateTime(date);
DateTime Interval2 = new DateTime();
Interval2=new DateTime(Interval);
//Now we have two joda time instances
Duration dd=new Duration(Interval1,Interval2);
//get duration between given and current time
Duration rd1=new Duration(1000);//seconds
Duration rd2=new Duration(60000);//minutes
Duration rd7=new Duration(60000*2);//2minutes
Duration rd3=new Duration(3600000);//hours
Duration rd6=new Duration(3600000*2);//2hours
Duration rd4=new Duration(86400000);//days
Duration rd5=new Duration(86400000*2);//2days
if(dd.isShorterThan(rd1)){
String k="just now";
return k;
}
else if(dd.isEqual(rd1)){
String k="just now";
return k;
}
else if(dd.isLongerThan(rd1)&& dd.isShorterThan(rd2))
{
String k=dd.getStandardSeconds()+""+" seconds ago";
return k;
}
else if(dd.isEqual(rd2) || dd.isShorterThan(rd7)){
String k=dd.getStandardMinutes()+""+" minute ago";
return k;
}
else if(dd.isLongerThan(rd2) && dd.isShorterThan(rd3)){
String k=dd.getStandardMinutes()+""+" minutes ago";
return k;
}
else if(dd.isEqual(rd3) || dd.isShorterThan(rd6))
{
String k=dd.getStandardHours()+""+" hour ago";
return k;
}
else if(dd.isLongerThan(rd3) && dd.isShorterThan(rd4)){
String k=dd.getStandardHours()+""+"hours ago";
return k;
}
else if(dd.isEqual(rd4) || dd.isShorterThan(rd5) ) {
String k=dd.getStandardDays()+""+" day ago";
return k;
}
else if(dd.isLongerThan(rd4)){
String k=dd.getStandardDays()+""+" days ago";
return k;
}
else{
String k="";
return k;
}
}