Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort LocalDateTime and get Max and min?

Tags:

java-8

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

like image 306
Vigneshwaran Avatar asked Oct 09 '17 01:10

Vigneshwaran


2 Answers

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.

like image 179
everton Avatar answered Nov 22 '22 21:11

everton


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);
    }
}
like image 43
Damith Avatar answered Nov 22 '22 20:11

Damith