Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java comparator for two LocalDateTimes

A custom class called MyCustomClass has a property that it s joda LocalDateTime. I need to create a java.util.Comparator class to compare instances of MyCustomClass by their TimeStamp property, which is of type LocalDateTime. I have read several postings on this (including this one), and I have tried all the methods, but none of the methods shown in the answers seem to work. For example, the following several approaches throw compilation errors:

import java.time.temporal.ChronoUnit;
import java.util.Comparator;

import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Period;

import my.app.model.MyCustomClass;

public class MyCustomClassComparator implements Comparator<MyCustomClass>{
    public int compare(MyCustomClass mcc1, MyCustomClass mcc2) {
        //this first attempt throws a return type error for the method.
        return Period.fieldDifference(mcc2.getTimestamp(), mcc1.getTimestamp());
        //This next attempt says that LocalDateTimes are not valid arguments
        Duration.between(mcc2.getTimestamp(), mcc1.getTimestamp());
        //The next approach also says LocalDateTimes are not valid arguments.
        DateTime.parse(mcc2.getTimestamp()), mcc1.getTimestamp()).getSeconds();
        //This boilerplate approach says the minus sign is not valid for LocalDateTime
        return mcc1.getTimestamp() - mcc2.getTimestamp();
    }
}

I intend to use this elsewhere in code like:

List<MyCustomClass> mccs = new ArrayList<MyCustomClass>();
// Sort by time stamp:
Collections.sort(mccs, new MyCustomClassComparator());

How do I write a Comparator class to compare instances of MyCustomClass based on their Joda LocalDateTime properties?

like image 424
CodeMed Avatar asked Dec 09 '22 01:12

CodeMed


2 Answers

If all you need is a comparator instance:

Similar to Iłya Bursov's answer, because LocalDateTime implements comparable, you can use Comparator.comparing

Comparator.comparing(MyCustomClass::getTimestamp)
like image 157
Alex Sadler Avatar answered Dec 10 '22 14:12

Alex Sadler


LocalDateTime implements comparable interface, so you can implement comparator like:

public class MyCustomClassComparator implements Comparator<MyCustomClass> {
    @Override
    public int compare(final MyCustomClass mcc1, final MyCustomClass mcc2) {
        return mcc1.getTimestamp().compareTo(mcc2.getTimestamp());
    }
}
like image 30
Iłya Bursov Avatar answered Dec 10 '22 13:12

Iłya Bursov