在java中獲取List集合中最大的日期時(shí)間操作
取List集合中最大的日期, 可以用Date max = Collections.max(dateList);, 傳入一個(gè)日期集合, 就可以獲取, 工作中有這個(gè)需求, 就查找到這個(gè),
代碼如下
} else {
/** 獲取此專(zhuān)題下的所有內(nèi)容的最新時(shí)間 */
Long featureId = this.communityFeatureMapper.selectFeatureIdByLabelId(labelId);
List<CommunityFeatureRelation> communityFeatureRelationList = this.communityFeatureRelationMapper.selectByFeatureId(featureId);
List<Date> 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);
/** 內(nèi)容->添加專(zhuān)題-此專(zhuān)題下的合伙人getReleaseTime 更新, 若此專(zhuān)題下不存在合伙人, 則不更新 */
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);
補(bǔ)充知識(shí):java自定義List中的sort()排序方法,用于日期排序
1、問(wèn)題描述
List是java中一個(gè)有序可重復(fù)的的集合,其中自帶的.sort()排序方法,該方法在針對(duì)純數(shù)字類(lèi)型List集合排序的時(shí)候很有效。但是對(duì)于裝入其他類(lèi)型的List集合,自帶的sort()方法排序我們很難控制,比如一個(gè)日期集合的排序。
2、解決方法:
java中List允許我們自定義sort()排序方法,以下自定義了List集合的sort排序方法,用于對(duì)一個(gè)字符串類(lèi)型的日期集合進(jìn)行排序。
//待排序的集合
List<String> list=new ArrayList<String>();
list.add("2019-06");
list.add("2019-11");
list.add("2019-02");
list.add("2019-09");
list.add("2019-05");
//自定義list排序,集合數(shù)據(jù)(月份)按升序排序;
final SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM");
Collections.sort(list, new Comparator<String>(){
@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;//調(diào)整順序,-1為不需要調(diào)整順序;
}
if(month1.equals(month2)){
mark = 0;
}
} catch (ParseException e) {
LOG.error("日期轉(zhuǎn)換異常", e);
e.printStackTrace();
}
return mark;
} //compare
});
3、其他
另外java兩個(gè)日期類(lèi)型的對(duì)象也可以用如下方法進(jìn)行比較。
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日期,返回的是一個(gè)布爾類(lèi)型結(jié)果。
以上這篇在java中獲取List集合中最大的日期時(shí)間操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringData Repository Bean方法定義規(guī)范代碼實(shí)例
這篇文章主要介紹了SpringData Repository Bean方法定義規(guī)范代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
自定義starter引發(fā)的線上事故記錄復(fù)盤(pán)
這篇文章主要為大家介紹了自定義starter引發(fā)的線上事故記錄復(fù)盤(pán),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
spring batch 讀取多個(gè)文件數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)示例
本篇文章主要介紹了spring batch 讀取多個(gè)文件數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
Java網(wǎng)絡(luò)編程UDP實(shí)現(xiàn)消息發(fā)送及聊天
這篇文章主要為大家詳細(xì)介紹了Java網(wǎng)絡(luò)編程UDP實(shí)現(xiàn)消息發(fā)送及聊天,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
Java中的SynchronousQueue隊(duì)列詳解
這篇文章主要介紹了Java中的SynchronousQueue隊(duì)列詳解,SynchronousQueue是BlockingQueue的一種,所以SynchronousQueue是線程安全的,SynchronousQueue和其他的BlockingQueue不同的是SynchronousQueue的capacity是0,需要的朋友可以參考下2023-12-12
Spring Cloud學(xué)習(xí)教程之DiscoveryClient的深入探究
這篇文章主要給大家介紹了關(guān)于Spring Cloud學(xué)習(xí)教程之DiscoveryClient的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
解決maven中只有Lifecycle而Dependencies和Plugins消失的問(wèn)題
這篇文章主要介紹了maven中只有Lifecycle而Dependencies和Plugins消失的問(wèn)題及解決方法,本文通過(guò)圖文的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-07-07
Spring事件發(fā)布監(jiān)聽(tīng),順序監(jiān)聽(tīng),異步監(jiān)聽(tīng)方式
這篇文章主要介紹了Spring事件發(fā)布監(jiān)聽(tīng),順序監(jiān)聽(tīng),異步監(jiān)聽(tīng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

