0

I need to compare a Calendar time to Date. I wrote below code.

Calendar now = Calendar.getInstance();
now.set(Calendar.SECOND, 0);
Date date = new Date();
date.setSeconds(0);
System.out.println(date);
System.out.println(now.getTime());
System.out.println(date.compareTo(now.getTime()));

Output is

Fri Dec 01 16:54:00 IST 2017
Fri Dec 01 16:54:00 IST 2017
1

It seems util date is bigger than calendar date. Why is it failing? what is the write way of comparing these dates?

Edit 1:- My problem is I need to compare a util date (stored in database) lets say 2017-12-01 16:41:00.0 to current Date and time, what is write approach?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
GD_Java
  • 1,359
  • 6
  • 24
  • 42
  • 4
    Could the milliseconds/nanoseconds be off? – nbokmans Dec 01 '17 at 11:31
  • 2
    Milliseconds rather than nanoseconds, but yes, that'll be what's wrong, I'm sure. Show `Date.getTime()` to make it clear exactly which values are being represented. That still won't *always* show 0 though, as the clock could "tick" between constructing the Calendar and constructing the Date. – Jon Skeet Dec 01 '17 at 11:32
  • 1
    Note: even if you correct this, the Date could have a newer time even after truncating the seconds as it occurs slightly later in this example. – Peter Lawrey Dec 01 '17 at 11:36
  • 1
    Be aware that [`Date.setSeconds`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#setSeconds-int-) is deprecated. Technicly, the `Date` class is/will be deprecated. – AxelH Dec 01 '17 at 11:45
  • Slightly aside, you shouldn’t want to compare the two at all. Both classes, `Date` and `Calendar` are long outdated and replaced by [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). And that doesn’t have the same distinction between date and calendar, so you will never have the issue. I warmly recommend you use `java.time` instead. – Ole V.V. Dec 01 '17 at 11:52

2 Answers2

2

Check the millis:

    System.out.println(now.getTime().getTime());
    System.out.println(date.getTime());
    System.out.println(now.getTime().getTime() - date.getTime());

Then you see that there (sometimes) is a difference.

Stefan
  • 2,395
  • 4
  • 15
  • 32
  • Yes I am getting a negative value. It means util date is bigger than calendar date. – GD_Java Dec 01 '17 at 11:34
  • Yes, you create `date` after you create `now`. So `date` will be bigger (or same as `now`). (If you like this answer, feel free to mark it as 'accepted'.) – Stefan Dec 01 '17 at 11:37
  • I understand what you said. But my problem is if I need to compare a util date (stored in database) to current Date and time, what is write approach? – GD_Java Dec 01 '17 at 11:39
  • You could compare now.getTime().getTime() / 1000 == date.getTime() / 1000 (even though it is not very pretty). – Stefan Dec 01 '17 at 11:44
1

A simple way to remove the seconds/millis is to count the number of minutes

// compare the minutes, ignoring the seconds/milli-seconds.
if (now.getTime().getTime() / 60_000 == date.getTime() / 60_000)

You can also use TimeUnit

if (TimeUnit.MILLISECONDS.toMinutes(now.getTime().getTime()) == 
    TimeUnit.MILLISECONDS.toMinutes(date.getTime()))

if you prefer to use a library than maths.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130