在java中获取List集合中最大的日期时间操作
取List集合中最大的日期,可以用Datemax=Collections.max(dateList);,传入一个日期集合,就可以获取,工作中有这个需求,就查找到这个,
代码如下
}else{ /**获取此专题下的所有内容的最新时间*/ LongfeatureId=this.communityFeatureMapper.selectFeatureIdByLabelId(labelId); ListcommunityFeatureRelationList=this.communityFeatureRelationMapper.selectByFeatureId(featureId); List dateList=Lists.newArrayList(); for(CommunityFeatureRelationcommunityFeatureRelation:communityFeatureRelationList){ CommunityProductcommunityProduct=this.communityProductMapper.selectContentIdByProductIdAndType(communityFeatureRelation.getProductId(),BaseConstans.ARTICLE_YIFABU); CommunityArticlecommunityArticle=this.communityArticleMapper.selectByPrimaryKey(communityProduct.getContentId()); dateList.add(communityArticle.getReleaseTime()); } if(!CollectionUtils.isEmpty(dateList)){ Datemax=Collections.max(dateList); /**内容->添加专题-此专题下的合伙人getReleaseTime更新,若此专题下不存在合伙人,则不更新*/ LongproductId=this.communityProductMapper.selectIdByContentIdAndType(featureId,BaseConstans.FOUR); CommunityPartnercommunityPartner=this.communityPartnerMapper.selectByPartnerId(productId); if(!StringUtils.isEmpty(communityPartner)){ communityPartner.setCreateTime(max); communityPartnerMapper.updateByPrimaryKeySelective(communityPartner); } } CommunityProductcommunityProduct=this.communityProductMapper.selectContentIdByProductIdAndType(prodId,proType);
补充知识:java自定义List中的sort()排序方法,用于日期排序
1、问题描述
List是java中一个有序可重复的的集合,其中自带的.sort()排序方法,该方法在针对纯数字类型List集合排序的时候很有效。但是对于装入其他类型的List集合,自带的sort()方法排序我们很难控制,比如一个日期集合的排序。
2、解决方法:
java中List允许我们自定义sort()排序方法,以下自定义了List集合的sort排序方法,用于对一个字符串类型的日期集合进行排序。
//待排序的集合 Listlist=newArrayList (); list.add("2019-06"); list.add("2019-11"); list.add("2019-02"); list.add("2019-09"); list.add("2019-05"); //自定义list排序,集合数据(月份)按升序排序; finalSimpleDateFormatsdft=newSimpleDateFormat("yyyy-MM"); Collections.sort(list,newComparator (){ @Override publicintcompare(Stringmonth1,Stringmonth2){ intmark=1; try{ Datedate1=sdft.parse(month1); Datedate2=sdft.parse(month2); if(date1.getTime() 3、其他
另外java两个日期类型的对象也可以用如下方法进行比较。
Date()date1=newDate(); Date()date2=newSimpleDateFormat("yyyy-MM-dd").parse("2019-06-11"); Booleanflag; if(date1.before(date2)){ flag=true; } a.before(b);该方法是判断a日期是否小于b日期,返回的是一个布尔类型结果。以上这篇在java中获取List集合中最大的日期时间操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。