Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find biggest and smallest date in a list?

I have collection of object as Collection basics = periodic.getGeneratedBasic(); When iterate over this collection and get every object and cast it, i can extract date of every object. Now at this point i want to check out in this collection of object which one is the smallness and biggest date.

Does any one know how to do that?

Date max;
Date min;
for(Object o:basics){
      Basic b = (Basic) o;
      Date temp;
      if(b.State=='U'){
           basicAList.add(ba);
           dateCollection.add(b.firstDateTime);
           temp= ;
           if(b.firstDateTime <)
     }
  }
like image 400
itro Avatar asked Oct 18 '12 09:10

itro


People also ask

How do you find the max and min date in python?

“How to get min and max date in Python” Code Answer's minimum=min(L1)#min() function helps to note smallest of the element. maximum=max(L1)#max() function helps to note biggest of the element.

Does math max work on dates JavaScript?

Returns the maximum of the given dates. Use the ES6 spread syntax with Math. max() to find the maximum date value.


2 Answers

In Java 8 you can do:

final Date maxDate = dates.stream()
    .max(Date::compareTo)
    .get();
like image 139
Krzysztof Wolny Avatar answered Sep 28 '22 06:09

Krzysztof Wolny


Date max = new Date(0);
Date min = new Date(0);
for(Object o:basics){
    Basic b = (Basic) o;
    if(b.State=='U'){
        basicAList.add(ba);
        dateCollection.add(b.firstDateTime);
        if(min.compareTo(b.firstDateTime) > 0) min = b.firstDateTime;
        else if(max.compareTo(b.firstDateTime) < 0) max = b.firstDateTime;
    }
}
like image 33
Reddy Avatar answered Sep 28 '22 07:09

Reddy