2

According to Java API public boolean before(Date when)

true if and only if the instant of time represented by this Date object is strictly earlier than the instant represented by when; false otherwise.

Now I have to check if date input by user is greater than current date then only it will accept the input otherwise throw exception so i tried below

if(userInputDate.before(new Date())){
      throw new Exception("Some Message");
     }

But if both date are same then also it going to inside if statement .Do it mean it will calculate time and then check rather than comparing date? If yes how to resolve my issue ?

Can any one tell me how to add check for this?

Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
  • 1
    what you have is equivalent to `if(userInputDate.getTime() < System.currentTimeMillis())` in case that helps you understand. (Both values are independant of timezones, they are ms since 1970 in UTC) – zapl Jun 02 '16 at 08:11
  • When you use `Date.before(Date t)` method, it checks every date parameter, including milliseconds. If you have created the date using `new Date()` , it creates a new date using the current time (almost accurate to the millisecond) so the chances that both dates are equal is very unlikely. So, in your case I would sugget you using (as @zapl has said) - `System.currentTimeMillis()` instead. – Asaf Savich Jun 02 '16 at 08:11
  • I think it is because the `Date` class contains also the milliseconds. Then, if you compare `userInputDate` with a `new Date()`, it is strictly probable that the first will be before the second. – riccardo.cardin Jun 02 '16 at 08:11
  • 1
    an extra condition of if `if(userInputDate.before(new Date()) && !userInputDate.equals(new Date())){..... }` – mattyman Jun 02 '16 at 08:43
  • perhaps u could change usage Date to JodaTime or Java1.8 new LocalDate/LocalDateTime classes? – T.G Jun 02 '16 at 09:05

2 Answers2

1

The method before in the class Date is comparing the millisecond between the dates, so it's not comparing just the day, it is comparing the instant of time.

You could create a method to check if the dates are not in the same day and the first date is before the second one

public static boolean isBeforeDate(Date date1, Date date2) {       
    SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
    boolean areTheSameDay = fmt.format(date1).equals(fmt.format(date2));
    return !areTheSameDay && date1.before(date2);
}

If you can use the new time api In Java8 instead of the old Date class, you can use the class LocalDate and the method compareTo:

boolean isBefore = myLocalDate.compareTo(myOtherLocalDate) < 0
David SN
  • 3,389
  • 1
  • 17
  • 21
0

All methods on java.util.Date that allow one to separate the time of day from the day of the year are deprecated. Therefore it is better to use java.util.Calendar. Additionally one should consider that there is typically one hour in each year that is in two days when summer time ends.

Here are two ways you can do it:

public static boolean isBeforeDay(Date date1, Date date2) {
    // convert date1 to noon on the same day
    Calendar day1 = Calendar.getInstance(TimeZone.getDefault());
    day1.setTime(date1);
    day1.set(Calendar.HOUR_OF_DAY, 12);
    day1.set(Calendar.MINUTE, 0);
    day1.set(Calendar.SECOND, 0);
    day1.set(Calendar.MILLISECOND, 0);

    // convert date2 to noon on the same day
    Calendar day2 = Calendar.getInstance(TimeZone.getDefault());
    day2.setTime(date2);
    day2.set(Calendar.HOUR_OF_DAY, 12);
    day2.set(Calendar.MINUTE, 0);
    day2.set(Calendar.SECOND, 0);
    day2.set(Calendar.MILLISECOND, 0);

    return day1.before(day2);
}


public static boolean isBeforeDay(Date date1, Date date2) {
    // get yyyymmdd value from date1
    Calendar day1 = Calendar.getInstance(TimeZone.getDefault());
    day1.setTime(date1);
    int ymd1 = 10000*day1.get(Calendar.YEAR) + 100*day1.get(Calendar.MONTH) + day1.get(Calendar.DAY_OF_MONTH);

    // get yyyymmdd value from date2
    Calendar day2 = Calendar.getInstance(TimeZone.getDefault());
    day2.setTime(date2);
    int ymd2 = 10000*day2.get(Calendar.YEAR) + 100*day2.get(Calendar.MONTH) + day2.get(Calendar.DAY_OF_MONTH);

    return ymd1 < ymd2;
}
Simon G.
  • 6,587
  • 25
  • 30