Comparing dates ignoring the seconds and the milliseconds instant of DateTime in Joda

14,516

Solution 1

I think you want to use DateTime#withMillisOfSecond() instead of DateTime#withMillis():

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-yyyy HH:mm:ss").withZone(DateTimeZone.forID("Asia/Kolkata"));
DateTime firstDate = formatter.parseDateTime("16-Feb-2012 12:03:45").withSecondOfMinute(0).withMillisOfSecond(0);
DateTime secondDate = formatter.parseDateTime("17-Feb-2013 12:03:45").withSecondOfMinute(0).withMillisOfSecond(0);

Setting DateTime#withMillis() to 0, will reset both your dates to 1/1/1970, and hence you get true for equals call on them.

Similarly, setting DateTime#withMillisOfDay() to 0, will set the time to midnight.

Solution 2

Another approach is to create a DateTimeComparator with minute as lower limit:

DateTimeComparator comparator = DateTimeComparator.getInstance(
         DateTimeFieldType.minuteOfHour());

which would ignore the second and millisecond parts of the objects being compared.

Share:
14,516
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 June 27, 2022

Comments

  • Tiny
    Tiny almost 2 years

    Let's assume that I have two dates like the following.

    DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-yyyy HH:mm:ss").withZone(DateTimeZone.forID("Asia/Kolkata"));
    DateTime firstDate = formatter.parseDateTime("16-Feb-2012 12:03:45");
    DateTime secondDate = formatter.parseDateTime("17-Feb-2013 12:03:45");
    

    I want to compare these two dates to see whether firstDate is sooner, later or equal to secondDate.

    I could try the following.

    System.out.println("firstDate = "+firstDate+"\nsecondDate = "+secondDate+"\ncomparison = "+firstDate.isBefore(secondDate));
    System.out.println("firstDate = "+firstDate+"\nsecondDate = "+secondDate+"\ncomparison = "+firstDate.isAfter(secondDate));
    System.out.println("firstDate = "+firstDate+"\nsecondDate = "+secondDate+"\ncomparison = "+firstDate.equals(secondDate));
    

    What is produced by this code is exactly what I want.

    It produces the following output.

    firstDate = 2012-02-16T12:03:45.000+05:30
    secondDate = 2013-02-17T12:03:45.000+05:30
    comparison = true
    
    firstDate = 2012-02-16T12:03:45.000+05:30
    secondDate = 2013-02-17T12:03:45.000+05:30
    comparison = false
    
    firstDate = 2012-02-16T12:03:45.000+05:30
    secondDate = 2013-02-17T12:03:45.000+05:30
    comparison = false
    

    I need to ignore the seconds and the milliseconds portion of these dates. I have tried to use the withSecondOfMinute(0) and withMillis(0) methods like the following.

    DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-yyyy HH:mm:ss").withZone(DateTimeZone.forID("Asia/Kolkata"));
    DateTime firstDate = formatter.parseDateTime("16-Feb-2012 12:03:45").withSecondOfMinute(0).withMillis(0);
    DateTime secondDate = formatter.parseDateTime("17-Feb-2013 12:03:45").withSecondOfMinute(0).withMillis(0);        
    

    But it yielded the following output.

    firstDate = 1970-01-01T05:30:00.000+05:30
    secondDate = 1970-01-01T05:30:00.000+05:30
    comparison = false
    
    firstDate = 1970-01-01T05:30:00.000+05:30
    secondDate = 1970-01-01T05:30:00.000+05:30
    comparison = false
    
    firstDate = 1970-01-01T05:30:00.000+05:30
    secondDate = 1970-01-01T05:30:00.000+05:30
    comparison = true
    

    The docs of the withSecondOfMinute() method describes.

    Returns a copy of this datetime with the second of minute field updated. DateTime is immutable, so there are no set methods. Instead, this method returns a new instance with the value of second of minute changed.

    And the docs of the method withMillis() says.

    Returns a copy of this datetime with different millis. The returned object will be either be a new instance or this. Only the millis will change, the chronology and time zone are kept.

    Comparing dates by ignoring the time portion completely can easily be done using DateTimeComparator.getDateOnlyInstance() roughly like the following.

    if(DateTimeComparator.getDateOnlyInstance().compare(firstDate, secondDate)==0){}
    if(DateTimeComparator.getDateOnlyInstance().compare(firstDate, secondDate)<0){}
    if(DateTimeComparator.getDateOnlyInstance().compare(firstDate, secondDate)>0){}
    

    How to compare two dates ignoring the specific instant in DateTime (seconds and milliseconds in this case)?

  • Tiny
    Tiny about 11 years
    I will try this approach later. Thank you.