栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在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);
	
	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集合中最大的日期时间操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/132602.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号