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集合中最大的日期时间操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 矩阵求逆_java实现的顺时针
- 下一篇: kinana 清空索引数据_(Elast