0

I'm trying to compare two dates of the same day.

The first in the format date1 = Fri Mar 24 00:00:00 CET 2017 the second is date2 = 2017-03-24 09:59:39.0

The date1 is retrieved from UI.

The date2 is rigistred in data base as a dateTime

The issue is when comparing the two dates, i always got the the value 1 when using the following method of Timestamp Class of java.sql package

date2.compareTo(date1); 

public int compareTo(Timestamp ts) {
        long thisTime = this.getTime();
        long anotherTime = ts.getTime();
        int i = (thisTime<anotherTime ? -1 :(thisTime==anotherTime?0 :1));
        if (i == 0) {
            if (nanos > ts.nanos) {
                    return 1;
            } else if (nanos < ts.nanos) {
                return -1;
            }
        }
        return i;

    }

I'm looking how to convert the date1 to a date of today with hours of midnight in order to have 0 as a return value.

pvg
  • 2,673
  • 4
  • 17
  • 31
Selma BA
  • 149
  • 3
  • 18
  • Can you use Java 8? Asking because I am speculating that it may be advantageous to convert the dates to `LocalDate`, class introduced in Java 8. – Ole V.V. Mar 24 '17 at 10:44
  • 1
    I think it is allready answerd in this question. http://stackoverflow.com/questions/1439779/how-to-compare-two-dates-without-the-time-portion –  Mar 24 '17 at 11:24
  • 1
    i'm using java 7 – Selma BA Mar 24 '17 at 12:24
  • In that case I would consider three options: (1) The `Calendar` class as in the second half of [Jorn’s answer](http://stackoverflow.com/a/1439845/5772882) (use `Calendar.setTime()` to convert from `Date` to `Calendar`) (2) Comparing formatted strings as in [Rob’s answer](http://stackoverflow.com/a/1712255/5772882); just use `String.compareTo()`. (3) `LocalDate` from [the ThreeTen Backport](http://www.threeten.org/threetenbp/); you can use it as I demonstrate in my answer. – Ole V.V. Mar 24 '17 at 12:36

1 Answers1

1

If you can, use the Java 8 date and time classes. It’s a long time ago I worked with JSF (JavaServer Faces was mentioned in your original question, someone edited it away), I believe it can now give you a LocalDate. A modern JDBC version should be able to give you a timestamp from the database as LocalDateTime or OffsetDateTime. Each of these has a toLocalDate method that will give you a LocalDate that you can compare to the one from the user. A LocalDate hasn’t got a time component, so all your problems will be solved. And it implements Comparable<LocalTime>. You have a choice between using compareTo(LocalTime), isBefore(LocalTime) or isAfter(LocalTime). Of course equals() works as expected too.

If from either your database or you UI you can only get an old-fashioned Date object, here’s how to convert it:

    LocalDate dateWithoutTime = yourDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

I am assuming that it is OK to use your computer’s time zone setting, which is what ZoneId.systemDefault() will give you. If not, provide the correct time zone there, for example ZoneId.of("Europe/Paris") for CET.

If you cannot use Java 8 at all, it can of course still be done. Look at the link that Ronald van der List provided. If you end up wanting to use a third party library, they say that you should prefer the ThreeTen Backport over JodaTime (I haven’t got experience with either).

Link to the ThreeTen Backport homepage: http://www.threeten.org/threetenbp/.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161