Alkis Kalogeris
Alkis Kalogeris

Reputation: 17745

Compare dates depending on format

I've built a custom data grid and I've added some filters. One filter I have is a date filter, for cells that display dates. My problem is that the cell might display

Displayed date value : 21/01/2014 , Actual date object value : 21-01-2014 16:32:0000

Of course the user doesn't know about the hour part, so when he/she provides a filter value of the same day, month and year expects to see the matched elements. This of course will not happen since the comparator, compares date objects that have different hour values.

What I want to ask is, is there a preferred way to compare two date objects depending on the display format? I can implement this logic, I was just curious if some implementation already existed.

Update

I have two date objects :

  1. 21-01-2014 16:32:0000
  2. 21-01-2014 00:00:0000

and a display format : "dd/MM/yyyy"

Is there a preferred way of combining these two dates depending on the format? This means only comparing the parts of the date that are included in the format. In the case above compare only day(dd), month(MM), year(yyyy).

I'm repeating I can implement something simple myself, I'm just curious if anyone else has encountered this before and has found a generic solution.

Upvotes: 1

Views: 1020

Answers (3)

Srini
Srini

Reputation: 1636

Use LocalDate for any date/time handling in Java. Read this post for why it's advantageous to use LocalDate over other older options.

Having said that, all you need to do to compare your Date(s) in the format you requested (dd/MM/yyyy) is this:

Let date1 and date2 be the Date objects that have your input dates.

LocalDate lD1 = new LocalDate(date1);
LocalDate lD2 = new LocalDate(date2);

String comparisonFormat = "dd/MM/yyyy";

boolean result = lD1.toString(comparisonFormat).equals(lD2.toString(comparisonFormat));

You could've done this in a one-liner if that's your thing but I think it's more readable this way.

Upvotes: 1

Igor Klimer
Igor Klimer

Reputation: 15321

If you are looking for a solution on the client side (where the Java code is compiled to Javascript by the GWT compiler), Krzysztof's solution will not work. GWT does not support Calendar and related classes on the client side. SimpleDateFormat is not supported either - DateTimeFormat has to be used instead.

There is however a useful utility class available in GWT: CalendarUtil. It has a isSameDate(java.util.Date date0, java.util.Date date1) method which does exactly what you want:

Check if two dates represent the same date of the same year, even if they have different times.

BTW, it also has a resetTime(java.util.Date date) method which could be useful in your use case:

Resets the date to have no time modifiers.

Upvotes: 1

Krzysztof Cichocki
Krzysztof Cichocki

Reputation: 6414

Before comparing modify the date in this fashion:

    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(date);
    gc.set(GregorianCalendar.HOUR_OF_DAY, 0);
    gc.set(GregorianCalendar.SECOND, 0);
    gc.set(GregorianCalendar.MINUTE, 0);
    gc.set(GregorianCalendar.MILLISECOND, 0);

    date = gc.getTime();

It will trim it to the same day value, so you can compare the two dates if it they are the same, but since you have trimmed them to day value, those dates will return true when compared regardless of the previous hours, minutes, seconds and milliseconds value.

Or you can do:

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    Date date1 = ...;

    Date date2 = ...;

    if (sdf.format(date1).equals(sdf.format(date2))) {
        // dates are equal in the day value
    }

Upvotes: 1

Related Questions