Comparing two dates from different format

Youssef :

How to compare this two dates from different format to check if they are equal

Date d1 = bean.fooDate; // >> from a post request
System.out.println(d1); // >> "Sat May 25 10:00:00 WET 2019"

Date d2 = fooEntity.getFooDate(); // >> from mysql database
System.out.println(d2); // >> "2019-05-25"

P.S i want just compare it as date not as datetime (no need to compare minutes & secondes)

Ole V.V. :

Saying that your two dates have different format isn’t really accurate. A Date hasn’t got a format. What you see what you print the dates is the return values from their toString methods.

Rather your dates have got different runtime types. d1 is an instance of java.util.Date and its toString method produced the format Sat May 25 10:00:00 WET 2019. d2in turn is an instance of java.sql.Date. It’s a subclass of java.util.Date, so can be assigned to a variable of this type. java.sql.Date.toString() produces the format 2019-05-25. However, the subclass (inheritance) relationship is a hack that you shouldn’t rely on in code.

The inheritance hack is just one out of many design problems with those two classes, though. Fortunately a newer and more modern replacement exists for both. For java.util.Date the modern Instant class is the most directly corresponding type, but exactly which one to use depends on your more precise requirements. For java.sql.Date it’s simpler, use LocalDate. Both Instant and LocalDate are part of java.time, the modern Java date and time API.

So see if you can replace bean.fooDate by an Instant or other appropriate modern type from java.time. Similarly see if you can get fooEntity.getFooDate() to return a LocalDate. Hibernate should be happy to handle LocalDate rather than Date for you (unless your Hibernate version is very old).

In any case, Srinivasan Sekar is correct that you should convert whatever type you get from each to LocalDate to compare only the dates and not the hours, minutes and seconds. LocalDate is exactly that: a date without time of day.

Say that you get an Instant from bean.fooDate. Then the conversion is:

    LocalDate localDate1 = bean.fooDate.atZone(ZoneId.of("Africa/El_Aaiun")).toLocalDate();

Be sure to specify an appropriate time zone.

If you cannot avoid an old-fashioned Date, the conversion in Srinivasan Sekar’s answer is correct:

    LocalDate localDate1 = bean.fooDate
            .toInstant()
            .atZone(ZoneId.of("Africa/El_Aaiun"))
            .toLocalDate();

For the case of fooEntity.getFooDate(), if you can get a LocalDate as mentioned, you’re done. If you can get only a Date, first check whether the method is declared to return a java.util.Date or a java.sql.Date. It probably depends on the import used in the file where the method is declared. The case of a java.sql.Date is simple enough (though unfortunately not guaranteed to give the correct date always):

    LocalDate localDate2 = fooEntity.getFooDate().toLocalDate();

If the declared return type is java.util.Date, you may just add a cast to the above since it seems that the actually returned object is a java.sql.Date. It would be safer to test your assumption first, though:

    LocalDate localDate2;
    Date d2 = fooEntity.getFooDate();
    if (d2 instanceof java.sql.Date) {
        localDate2 = ((java.sql.Date) fooEntity.getFooDate()).toLocalDate();
    } else {
        localDate2 = d2.toInstant()
                .atZone(ZoneId.of("Africa/El_Aaiun"))
                .toLocalDate();
    }

You will recognize that in the second case I am using the same conversion as above.

To compare the resulting LocalDate variables:

    if (localDate1.isEqual(localDate2)) {
        System.out.println("Same date");
    } else {
        System.out.println("Different dates");
    }

LocalDate also has methods isBefore and isAfter in case you need to determine which is earlier.

Link: Oracle tutorial: Date Time explaining how to use java.time.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=329675&siteId=1