Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing instances of java.time.ZonedDateTime ignoring seconds and milliseconds instants from comparisons in Java 8

I am looking for an equivalent way of Joda Time in Java 8 comparing instances of org.joda.time.DateTime (with a time zone specified) ignoring seconds and milliseconds from comparisons as follows.

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-yyyy hh:mm:ss:SSS a Z").withZone(DateTimeZone.forID("Asia/Kolkata"));
DateTime first = formatter.parseDateTime("16-Feb-2012 12:03:45:999 AM +05:30");
DateTime second = formatter.parseDateTime("16-Feb-2012 12:03:55:999 AM +05:30");

DateTimeComparator comparator = DateTimeComparator.getInstance(DateTimeFieldType.minuteOfHour());
int compare = comparator.compare(first, second);
System.out.println("compare : " + compare);

The comparison returns 0 meaning that both the objects have been considered equal after ignoring seconds and milliseconds instants from the comparison.

Fields of a magnitude less than the lower limit specified with DateTimeFieldType are ignored here.

What is the equivalent way of doing the same thing using the Java Time API in Java 8?

To be honest, I did not succeed to achieve the same in Java 8 with my attempts.

like image 727
Tiny Avatar asked Jan 18 '16 02:01

Tiny


People also ask

How does ZonedDateTime compare?

The comparison is based first on the instant, then on the local date-time, then on the zone ID, then on the chronology. So compareTo() compares all 4 information in both instances. It returns a value negative if less, positive if greater and 0 if both date-time instances are equal. ZonedDateTime zdtNow = ZonedDateTime.

How can I compare two dates in java8?

Example: Java Compare two dates Using Date. We can use compareTo() function from Date class to compare the two dates. compareTo() function returns: 0 if both dates are equal. 1 if date1 comes after date2.

What is ZonedDateTime in Java?

ZonedDateTime is an immutable representation of a date-time with a time-zone. This class stores all date and time fields, to a precision of nanoseconds, and a time-zone, with a zone offset used to handle ambiguous local date-times.

How do I get time from ZonedDateTime?

now() now() method of a ZonedDateTime class used to obtain the current date-time from the system clock in the default time-zone. This method will return ZonedDateTime based on system clock with default time-zone to obtain the current date-time.


1 Answers

As Java-8 introduced lambdas and method references, having dedicated Comparator classes became mostly unnecessary, so they are absent in java.time. You may write instead:

Comparator<ZonedDateTime> comparator = Comparator.comparing(
      zdt -> zdt.truncatedTo(ChronoUnit.MINUTES));

Complete example:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy hh:mm:ss:SSS a X")
        .withLocale(Locale.ENGLISH).withZone(ZoneId.of("Asia/Kolkata"));
ZonedDateTime first = ZonedDateTime.parse("16-Feb-2012 12:03:45:999 AM +0530", formatter);
ZonedDateTime second = ZonedDateTime.parse("16-Feb-2012 12:03:55:999 AM +0530", formatter);
Comparator<ZonedDateTime> comparator = Comparator.comparing(
        zdt -> zdt.truncatedTo(ChronoUnit.MINUTES));
System.out.println(comparator.compare(first, second));
like image 126
Tagir Valeev Avatar answered Sep 20 '22 11:09

Tagir Valeev