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

11,197

Solution 1

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));

Solution 2

Try ChronoUnit class.

long minutes = ChronoUnit.MINUTES.between(first, second);
Share:
11,197
Tiny
Author by

Tiny

Just an orphan kid and have no more to say. Three things in general, cannot be avoided (at least I can never) Mother Mother-tongue Mother-land. They are always unique. I'm a family-less boy. My family was hunted leaving me all alone when my house targeted and deliberately set on a fire by a mob during a nonsense communal riot but I was survived by a rescue team with the help of firemen. As a survival, I didn't know whether it was my fortune or misfortune but when I recovered, the rescue team came to my home, one day. One of the members gave me a piece of paper in my hand in which the following text was written. lifeisnowhere. He asked me to read it carefully and I could hardly interpret the text as Life is now here, instead of Life is nowhere. All of them gave me a cute smile and went away and I decided to live peacefully and hopefully on their saying from then onwards and very soon. Because of this tragedy, I'm alone couldn't join a school but a curiosity to learn something made me a self-learner. I'm indeed a self-learner, so I'm likely not able to answer any questions on this site right now. In the field of computer science, my self-study mainly includes, QBASIC, C, C++, C#, VB, Java, JavaScript, PHP and a little about ASP.NET. Oracle, MySQL and MSSQL-Server with DBMS. and other theoretical subjects. I'm currently dealing with - Android and Java EE including Servlet, JSP-JSTL/EL (with Spring and Struts with ORM models JPA/Hibernate) and JSF.

Updated on July 22, 2022

Comments

  • Tiny
    Tiny almost 2 years

    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.