当前位置: 移动技术网 > IT编程>开发语言>Java > 在java中获取List集合中最大的日期时间操作

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

2020年08月16日  | 移动技术网IT编程  | 我要评论

取list集合中最大的日期, 可以用date max = collections.max(datelist);, 传入一个日期集合, 就可以获取, 工作中有这个需求, 就查找到这个,

代码如下

} else {
 
/** 获取此专题下的所有内容的最新时间 */
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);
	/** 内容->添加专题-此专题下的合伙人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<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排序,集合数据(月份)按升序排序;
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;//调整顺序,-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集合中最大的日期时间操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网