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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值