0

From java class Date docs:

before(Date when) Tests if this date is before the specified date.

When I use this method to test whether selected date is equal to today, I get wrong output message.

public class JavaApplication28 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws NoSuchAlgorithmException, ParseException {

        Date date1;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String statusDT ="2018-04-08";
        date1 = formatter.parse(statusDT);
        if (date1.equals(new Date())) {
            System.out.println("today");
        } else if (date1.before(new Date())) {
            System.out.println("wrong");
        }
    }
}

This is date1, which is today date

Sun Apr 08 00:00:00 MYT 2018

The equal method look not functiong as well

equals(Object obj) Compares two dates for equality.

John Joe
  • 12,412
  • 16
  • 70
  • 135
  • 1
    can you show us the full code please, so we can test it? – Youcef LAIDANI Apr 08 '18 at 13:02
  • it would be really useful what `statusDT` is – leonardkraemer Apr 08 '18 at 13:04
  • @YCF_L post edited – John Joe Apr 08 '18 at 13:07
  • @leoderprofi please check – John Joe Apr 08 '18 at 13:07
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 08 '18 at 22:53

3 Answers3

2
  • The given date date1 represent today but with midnight time : Sun Apr 08 00:00:00 MYT 2018
  • the date used for comparison new Date() represents also today but the actual time (about 15h10) : Sun Apr 08 15:10:00 MYT 2018

So date1 if before actual Date() and it goes in the good if section


As the Java8 introduce a new Date API, it's easier to use in most case :

  • LocalDateTime which holds Date (day/month/year) and Time (sec/min/hour)
  • LocalDate which holds the Date part and can be given from a LocalDateTime.toLocalDate()
  • LocalTime which holds the Time part and can be given from a LocalDateTime.toLocalTime()

So if you don't matter of the time, and just want to check the day/month/year you can use only the LocalDate part from the LocalDateTime :

if (date1.toLocalDate().equals(LocalDate.now())) {
    System.out.println("today");                              //< ---
} else if (date1.toLocalDate().isBefore(LocalDate.now())) {
    System.out.println("before now");
} else if (date1.toLocalDate().isAfter(LocalDate.now())) {
    System.out.println("after now");
}
azro
  • 53,056
  • 7
  • 34
  • 70
2

Try this :

long l1 = date1.getTime();
long l2 = (new Date()).getTime();

Output

1523142000000
1523192849177

The doc said :

Compares two dates for equality. The result is true if and only if the argument is not null and is a Date object that represents the same point in time, to the millisecond, as this object. Thus, two Date objects are equal if and only if the getTime method returns the same long value for both.

Your get wrong because you thing that equal compare only the date part, but NO, it also compare the time part


Another Solution

Because you are using Java 8 why not using java.time instead like this :

String statusDT = "2018-04-08";
LocalDate date = LocalDate.parse(statusDT, DateTimeFormatter.ofPattern("yyyy-MM-dd"));

if (date.isEqual(LocalDate.now())) {
    System.out.println("today");
} else if (date.isBefore(LocalDate.now())) {
    System.out.println("before");
} else {
    System.out.println("after");
}
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
-1

Class java.util.Date contains both a date and a time-of-day - as can be seen in the output of your program. The Date you obtain by parsing the String has no time-of-day, only a date. In other words, its time-of-day is 00:00 (i.e. midnight). Hence the two Dates are not equeal.

And by the way, the link you provided for the javadoc of the Date class is the Java 8 documentation. If this means you are using Java 8, then there is a new Date-Time API. There is a tutorial at https://docs.oracle.com/javase/tutorial/datetime/index.html

Abra
  • 19,142
  • 7
  • 29
  • 41