java web 文章,论坛类列表实现时间一天前,一月前,一年前。。

实现效果:

String hql = "select s.id,s.questionTitle,s.questionContent,u.userName,s.answerNumber,s.pageView,(select count(*)

from SQuestionInfo s,SCommonQuestion c where s.id = c.questionId),s.createTime,0 as times

from SQuestionInfo s,UUserInfo u where s.userId = u.id order by s.createTime desc";

List questionList = getListByPage(hql, currentPage, pageSize);//查询出list

if(null != questionList && questionList.size()>0 ){

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date now = new Date();

for (int i = 0; i < questionList.size(); i++) { try { //now = df.parse("2004-01-02 11:30:25"); Object[] objects = (Object[]) questionList.get(i); java.util.Date date=df.parse(objects[7].toString());//问题创建时间 long l=now.getTime()-date.getTime(); long year = l/(12*4*7*24*60*60)/1000;//这里如果直接把1000放在*60后面会出现算出的数值不对,为负数,所以拿出来 long month = (l/(4*7*24*60*60)/1000 – (year<0?Math.abs(year):year*12)); long week = (l/(7*24*60*60)/1000 -(year<0?Math.abs(year):year*12*4) – (month<0?Math.abs(month):month*4)); long day=(l/(24*60*60)/1000 -(year<0?Math.abs(year):year*12*4*7)-(month<0?Math.abs(month):month*4*7)-(week<0?Math.abs(week):week*7)); long hour=(l/(60*60)/1000 -(year<0?Math.abs(year):year*12*4*7*24)-(month<0?Math.abs(month):month*4*7*24)-(week<0?Math.abs(week):week*7*24)-(day<0?Math.abs(day):day*24)); long min=(l/(60*1000)-(year<0?Math.abs(year):year*12*4*7*24*60)-(month<0?Math.abs(month):month*4*7*24*60)-(week<0?Math.abs(week):week*7*24*60)-(day<0?Math.abs(day):day*24*60)-(hour<0?Math.abs(hour):hour*60)); long s=(l/1000-(year<0?Math.abs(year):year*12*4*7*24*60*60)-(month<0?Math.abs(month):month*4*7*24*60*60)-(week<0?Math.abs(week):week*7*24*60*60)-(day<0?Math.abs(day):day*24*60*60)-(hour<0?Math.abs(hour):hour*60*60)-(min<0?Math.abs(min):min*60)); StringBuffer sb = new StringBuffer(); sb.append("发表于:"); if(year > 0){ sb.append(year+"年前"); objects[8] = sb.toString(); }else{ if(month > 0){ sb.append(month+"月前"); objects[8] = sb.toString(); }else{ if(week > 0){ sb.append(week+"周前"); objects[8] = sb.toString(); }else{ if(day > 0){ sb.append(day+"天前"); objects[8] = sb.toString(); }else{ if(hour > 0 ){ sb.append(hour+"小时前"); objects[8] = sb.toString(); }else{ if(min > 0 ){ sb.append(min+"分钟前"); objects[8] = sb.toString(); }else{ if(s > 0){ sb.append(s+"秒前"); objects[8] = sb.toString(); } } } } } } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }

}

return questionList;只要功夫深,铁棒磨成绣花针。

java web  文章,论坛类列表实现时间一天前,一月前,一年前。。

相关文章:

你感兴趣的文章:

标签云: