How to sort LocalDateTime and get Max and min?

22,692

Solution 1

Assume that your Domain Object name as "Employee" and have List<Employee> as the list of Domain Objects. Then, you may use Java 8 sort function by passing a Comparator as follows. Subsequently, you shall extract Min and Max as you wish.

employeeList.sort((Employee e1, Employee e2) -> e1.getUpdatedDate().compareTo(e2.getUpdatedDate()));

Find the below code sample for your reference.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

public class Main {
    static DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssxx");

    static Employee emp1 = new Employee(1, LocalDateTime.parse("2017-10-06 23:25:37+0000", format));
    static Employee emp2 = new Employee(2, LocalDateTime.parse("2016-07-09 00:00:00+0000", format));
    static Employee emp3 = new Employee(3, LocalDateTime.parse("2017-10-06 23:25:38+0000", format));
    static Employee emp4 = new Employee(4, LocalDateTime.parse("2016-07-09 00:00:00+0000", format));

    static List<Employee> employeeList = Arrays.asList(new Employee[] { emp1, emp2, emp3, emp4 });

    public static void main(String[] args) {
        employeeList.sort((Employee e1, Employee e2) -> e1.getUpdatedDate().compareTo(e2.getUpdatedDate()));

        System.out.println("Max:" + employeeList.get(employeeList.size() - 1));
        System.out.println("Min:" + employeeList.get(0));

        System.out.println("==== Complete list ===");
        employeeList.forEach(e -> System.out.println(e));

    }
}

class Employee {
    private int employeeNumber;
    private LocalDateTime updatedDate;

    public Employee() {
    }

    public Employee(int employeeNumber, LocalDateTime updatedDate) {
        this.employeeNumber = employeeNumber;
        this.updatedDate = updatedDate;
    }

    public int getEmployeeNumber() {
        return employeeNumber;
    }

    public void setEmployeeNumber(int employeeNumber) {
        this.employeeNumber = employeeNumber;
    }

    public LocalDateTime getUpdatedDate() {
        return updatedDate;
    }

    public void setUpdatedDate(LocalDateTime updatedDate) {
        this.updatedDate = updatedDate;
    }

    public String toString() {
        return String.format("Employee Number: %s, Updated Date: %s", this.employeeNumber, this.updatedDate);
    }
}

Solution 2

Your simplified dataset

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssxx");
dates.add(LocalDateTime.parse("2016-07-09 00:00:00+0000", fmt));
dates.add(LocalDateTime.parse("2017-10-06 23:25:37+0000", fmt));

Converting the hashset to streams and producing min and max out of it

System.out.println("Min: " + dates.stream().min(LocalDateTime::compareTo));
System.out.println("Max: " + dates.stream().max(LocalDateTime::compareTo));

Output:

Min: 2016-07-09T00:00
Max: 2017-10-06T23:25:37

You can easily adapt this core logic to your scenario now.

Share:
22,692
Vigneshwaran
Author by

Vigneshwaran

Updated on July 05, 2022

Comments

  • Vigneshwaran
    Vigneshwaran almost 2 years

    Can anyone share the logic to sort the LocalDateTime and get max and min record from list of domain object.

    Domain Object-

    private int employeeNumber;
    private LocalDateTime updatedDate;
    

    First Record

    1.1,2016-07-09 00:00:00+0000
    2.2,2017-10-06 23:25:37+0000
    

    Max Output: 2,2017-10-06 23:25:37+0000

    Min Output: 1,2016-07-09 00:00:00+0000

  • Parag Kadam
    Parag Kadam almost 3 years
    How can I use the above code if in place of dates there are employees which has a property lastSeen which is java.sql.Timestamp and I want to get an employee with the highest lastSeen?