Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joda time: DateTimeComparator. What is similar in Java 8 Time Api?

With Joda Time, you are able to do a really cool things, for example:

package temp;

import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;
import org.joda.time.DateTimeFieldType;

public class TestDateTimeComparator {

    public static void main(String[] args) {

        //Two DateTime instances which have same month, date, and hour
        //but different year, minutes and seconds

        DateTime d1 = new DateTime(2001,05,12,7,0,0);
        DateTime d2 = new DateTime(2014,05,12,7,30,45);

        //Define the lower limit to be hour and upper limit to be month
        DateTimeFieldType lowerLimit = DateTimeFieldType.hourOfDay();
        DateTimeFieldType upperLimit = DateTimeFieldType.monthOfYear();

        //Because of the upper and lower limits , the comparator shall only consider only those sub-elements
        //within the lower and upper limits i.e.month, day and hour
        //It shall ignore those sub-elements outside the lower and upper limits: i.e year, minute and second
        DateTimeComparator dateTimeComparator = DateTimeComparator.getInstance(lowerLimit,upperLimit);
        int result = dateTimeComparator.compare(d1, d2);

        switch (result) {
        case -1:
            System.out.println("d1 is less than d2");
            break;
        case 0:
            System.out.println("d1 is equal to d2");
            break; 
        case 1:
            System.out.println("d1 is greater than d2");
            break;

        default:
            break;
        }
    }
}

I found this example here.

I want to have the same steps but with Java Time API, but unfortunately, I do not see any similar Comparators.

How can I compare only certain date and time fields but not others with Java Time API?

like image 446
user471011 Avatar asked Jan 28 '16 20:01

user471011


People also ask

What is the replacement of Joda-time Library in Java 8?

Correct Option: D. In java 8,we are asked to migrate to java. time (JSR-310) which is a core part of the JDK which replaces joda library project.

Is Joda-time format followed in Java 8?

Joda-Time is an API created by joda.org which offers better classes and having efficient methods to handle date and time than classes from java. util package like Calendar, Gregorian Calendar, Date, etc. This API is included in Java 8.0 with the java.

What is Joda-time used for?

Joda-Time is the most widely used date and time processing library, before the release of Java 8. Its purpose was to offer an intuitive API for processing date and time and also address the design issues that existed in the Java Date/Time API.


1 Answers

You can replicate some of this behavior, a bit more manually, with the general-purpose helper methods provided on Comparator.

Assuming that we import static java.util.Comparator.comparing;, we can define a comparator on LocalDateTimes that compares only the month:

Comparator<LocalDateTime> byMonth = comparing(LocalDateTime::getMonth);

or one that compares only the month, day, and hour, as in your example:

Comparator<LocalDateTime> byHourDayMonth = comparing(LocalDateTime::getMonth) //
  .thenComparing(LocalDateTime::getDayOfMonth) //
  .thenComparing(LocalDateTime::getHour);

This does leave you in the position of manually deciding the order... not quite as automatic but with some more fine-grained control.

like image 67
Sam Lindsay-Levine Avatar answered Oct 20 '22 18:10

Sam Lindsay-Levine