自定义反向比较器reverseSort

本文介绍了一个自定义的反向排序比较器实现,用于按日期倒序排列论坛信息,并提供了一个实用的方法来判断实体类中的日期是否超过当前时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**

 * 自定义反向排序比较器
 */
public class ReverseSort implements Comparator {
/**日期倒序*/
    public int compare(Object obj1, Object obj2) {
    if(obj1 instanceof BbsInformation && obj2 instanceof BbsInformation){
    BbsInformation b1 =(BbsInformation)obj1;
    BbsInformation b2 =(BbsInformation)obj2;
    if(null!=b1.getBegintime()&& null!=b2.getBegintime()){
    return -b1.getBegintime().compareTo(b2.getBegintime());
    }else{
    return 0;
    }
    }
    return 0;
    }

/**和当前日期比较
* entityDate实体类时间 
* 超时返回false
* */
public static boolean compareTime(Date entityDate){
Date nowTime= new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(entityDate);
Date endDate =cal.getTime();
if(endDate.compareTo(nowTime)<0){//超时
return false;
}
return true;
}

}

使用

ReverseSort reverseSort = new ReverseSort();
reverseSort.compare(obj1, obj2);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值