java List最大_在java中获取List集合中最大的日期时间操作
取list集合中最大的日期, 可以用date max = collections.max(datelist);, 傳入一個(gè)日期集合, 就可以獲取, 工作中有這個(gè)需求, 就查找到這個(gè),
代碼如下
} else {
/** 獲取此專(zhuān)題下的所有內(nèi)容的最新時(shí)間 */
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);
/** 內(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中l(wèi)ist允許我們自定義sort()排序方法,以下自定義了list集合的sort排序方法,用于對(duì)一個(gè)字符串類(lèi)型的日期集合進(jìn)行排序。
//待排序的集合
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排序,集合數(shù)據(jù)(月份)按升序排序;
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;//調(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è)參考,也希望大家多多支持萬(wàn)仟網(wǎng)。
如您對(duì)本文有疑問(wèn)或者有任何想說(shuō)的,請(qǐng)點(diǎn)擊進(jìn)行留言回復(fù),萬(wàn)千網(wǎng)友為您解惑!
總結(jié)
以上是生活随笔為你收集整理的java List最大_在java中获取List集合中最大的日期时间操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java 矩阵求逆_java实现的顺时针
- 下一篇: java 句柄 内存_Java内存区域学