0

I am building an app using hibernate(spring-jpa more specifically). My model class contain a java.util.Date field and I want to filter the records based on the date field ignoring the time part.

I try to achieve this with spring-jpa specification but for equal operation it always returns 0 objects

I further debug this problem and found that when hibernate return the Object with field type is java.sql.timeStamp where I compare it with java.util.Date so it never matched.

Below is sample code I used for debugging

       List<EmployeeLeaves> l = empLeaveDao.findAll();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-DD");
        Date date = formatter.parse("2018-04-28");
        long time = date.getTime();
        date = new java.sql.Timestamp(time);

        for (EmployeeLeaves d : l) {
            System.out.println(d.getAppliedOn().getClass().getName());
            if (d.getAppliedOn().equals(date)) {
                System.out.println("==============" + d.getRecordId());
            }
        }
        return l;

Still no luck as two date Objects never match as equals method never returns true.

Can anyone suggest how to create a new Date object so It can equal with Date objects returned by database.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Prasad Parab
  • 437
  • 1
  • 7
  • 26
  • what type is the date on EmployeeLeaves ? – Maciej Kowalski Jul 11 '19 at 07:01
  • Its java.util.Date but when I retrieved it from DB I get java.sql.timestamp(my table column is of type Date) – Prasad Parab Jul 11 '19 at 07:12
  • I recommend you don’t use `SimpleDateFormat`, `Date` and `Timestamp`. Those classes are poorly designed and long outdated, the first in particular notoriously troublesome. Instead use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Unless you have a very old Spring JPA version, it should be able to handle types from java.time nicely. I think that `LocalDate` is the modern class that you are after. – Ole V.V. Jul 11 '19 at 08:24

1 Answers1

1

You can use function Date.compareTo() as in this artile enter link description here

I also tested with this small code snippet, it works as expected

Date date = new Date();
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
System.out.println(date.compareTo(sqlDate) == 0);

Also if you use java8, why don't you change the date type in Entity class to LocalDate and you would not have this kind of problem?

  • 1
    I can’t get this to work in any useful way. Your code snippet is comparing two `Date` objects having the same millisecond value (though probably two different `Date` classes from `java.util` and `java.sql`). There’s no surprise that this yields 0. How could this solve the questioner’s problem? – Ole V.V. Jul 11 '19 at 10:14
  • It works by changing new `SimpleDateFormat("yyyy-MM-DD");` to new `SimpleDateFormat("yyyy-MM-dd");` – Prasad Parab Jul 11 '19 at 10:39
  • This Answer uses terrible date-time classes that were supplanted years ago by the modern *java.time* classes with the adoption of JSR 310. – Basil Bourque Jul 12 '19 at 04:46
  • 1
    @BasilBourque That's why i recommended him to use java 8 's LocalDate – Nguyen Tan Bao Jul 12 '19 at 05:37