java list中最大的_在java中获取List集合中最大的日期时间操作

在java中获取List集合中最大的日期时间操作,日期,方法,自定义,类型,专题

在java中获取List集合中最大的日期时间操作

易采站长站,站长之家为您整理了在java中获取List集合中最大的日期时间操作的相关内容。

取List集合中最大的日期, 可以用Date max = Collections.max(dateList);, 传入一个日期集合, 就可以获取, 工作中有这个需求, 就查找到这个,

代码如下

} else {

/** 获取此专题下的所有内容的最新时间 */

Long featureId = this.communityFeatureMapper.selectFeatureIdByLabelId(labelId);

List communityFeatureRelationList = this.communityFeatureRelationMapper.selectByFeatureId(featureId);

List dateList = Lists.newArrayList();

for (CommunityFeatureRelation communityFeatureRelation : communityFeatureRelationList) {

CommunityProduct communityProduct =this.communityProductMapper.selectContentIdByProductIdAndType(communityFeatureRelation.getProductId(),BaseConstans.ARTICLE_YIFABU);

CommunityArticle communityArticle = this.communityArticleMapper.selectByPrimaryKey(communityProduct.getContentId());

dateList.add(communityArticle.getReleaseTime());

}

if (!CollectionUtils.isEmpty(dateList)) {

Date max = Collections.max(dateList);

/** 内容->添加专题-此专题下的合伙人getReleaseTime 更新, 若此专题下不存在合伙人, 则不更新 */

Long productId = this.communityProductMapper.selectIdByContentIdAndType(featureId, BaseConstans.FOUR);

CommunityPartner communityPartner = this.communityPartnerMapper.selectByPartnerId(productId);

if (!StringUtils.isEmpty(communityPartner)) {

communityPartner.setCreateTime(max);

communityPartnerMapper.updateByPrimaryKeySelective(communityPartner);

}

}

CommunityProduct communityProduct = this.communityProductMapper.selectContentIdByProductIdAndType(prodId, proType);

补充知识:java自定义List中的sort()排序方法,用于日期排序

1、问题描述

List是java中一个有序可重复的的集合,其中自带的.sort()排序方法,该方法在针对纯数字类型List集合排序的时候很有效。但是对于装入其他类型的List集合,自带的sort()方法排序我们很难控制,比如一个日期集合的排序。

2、解决方法:

java中List允许我们自定义sort()排序方法,以下自定义了List集合的sort排序方法,用于对一个字符串类型的日期集合进行排序。

//待排序的集合

List list=new ArrayList();

list.add("2019-06");

list.add("2019-11");

list.add("2019-02");

list.add("2019-09");

list.add("2019-05");

//自定义list排序,集合数据(月份)按升序排序;

final SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM");

Collections.sort(list, new Comparator(){

@Override

public int compare(String month1, String month2) {

int mark = 1;

try {

Date date1 = sdft.parse(month1);

Date date2 = sdft.parse(month2);

if(date1.getTime() < date2.getTime()){

mark = -1;//调整顺序,-1为不需要调整顺序;

}

if(month1.equals(month2)){

mark = 0;

}

} catch (ParseException e) {

LOG.error("日期转换异常", e);

e.printStackTrace();

}

return mark;

} //compare

});

3、其他

另外java两个日期类型的对象也可以用如下方法进行比较。

Date() date1=new Date();

Date() date2=new SimpleDateFormat("yyyy-MM-dd").parse("2019-06-11");

Boolean flag;

if(date1.before(date2)){

flag=true;

}

a.before(b);该方法是判断a日期是否小于b日期,返回的是一个布尔类型结果。

以上这篇在java中获取List集合中最大的日期时间操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易采站长站。以上就是关于对在java中获取List集合中最大的日期时间操作的详细介绍。欢迎大家对在java中获取List集合中最大的日期时间操作内容提出宝贵意见

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值