Is a calendar or date after/before today or within a number of days in the future

 
import java.util.Calendar;
import java.util.Date;

public class Main {
  public static boolean isBeforeDay(Date date1, Date date2) {
    if (date1 == null || date2 == null) {
      throw new IllegalArgumentException("The dates must not be null");
    }
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);
    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);
    return isBeforeDay(cal1, cal2);
  }

  public static boolean isBeforeDay(Calendar cal1, Calendar cal2) {
    if (cal1 == null || cal2 == null) {
      throw new IllegalArgumentException("The dates must not be null");
    }
    if (cal1.get(Calendar.ERA) < cal2.get(Calendar.ERA))
      return true;
    if (cal1.get(Calendar.ERA) > cal2.get(Calendar.ERA))
      return false;
    if (cal1.get(Calendar.YEAR) < cal2.get(Calendar.YEAR))
      return true;
    if (cal1.get(Calendar.YEAR) > cal2.get(Calendar.YEAR))
      return false;
    return cal1.get(Calendar.DAY_OF_YEAR) < cal2.get(Calendar.DAY_OF_YEAR);
  }

  public static boolean isAfterDay(Date date1, Date date2) {
    if (date1 == null || date2 == null) {
      throw new IllegalArgumentException("The dates must not be null");
    }
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);
    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);
    return isAfterDay(cal1, cal2);
  }

  public static boolean isAfterDay(Calendar cal1, Calendar cal2) {
    if (cal1 == null || cal2 == null) {
      throw new IllegalArgumentException("The dates must not be null");
    }
    if (cal1.get(Calendar.ERA) < cal2.get(Calendar.ERA))
      return false;
    if (cal1.get(Calendar.ERA) > cal2.get(Calendar.ERA))
      return true;
    if (cal1.get(Calendar.YEAR) < cal2.get(Calendar.YEAR))
      return false;
    if (cal1.get(Calendar.YEAR) > cal2.get(Calendar.YEAR))
      return true;
    return cal1.get(Calendar.DAY_OF_YEAR) > cal2.get(Calendar.DAY_OF_YEAR);
  }

  public static boolean isWithinDaysFuture(Date date, int days) {
    if (date == null) {
      throw new IllegalArgumentException("The date must not be null");
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return isWithinDaysFuture(cal, days);
  }

  public static boolean isWithinDaysFuture(Calendar cal, int days) {
    if (cal == null) {
      throw new IllegalArgumentException("The date must not be null");
    }
    Calendar today = Calendar.getInstance();
    Calendar future = Calendar.getInstance();
    future.add(Calendar.DAY_OF_YEAR, days);
    return (isAfterDay(cal, today) && !isAfterDay(cal, future));
  }

}
  
Home 
  Java Book 
    Runnable examples  

Date Compare:
  1. Are two calendar objects represent the same local time.
  2. Are two dates or two calendars the same day
  3. Is a date the same day with another date
  4. Is a date after another date
  5. Is a date before another date
  6. Is date or calendar today's date
  7. Is a calendar or date after/before today or within a number of days in the future
  8. Compare two Date objects using compareTo
  9. Compares two dates are equals at day, month and year level, ignoring time
  10. Compare two times equals regardless of the date
  11. Compare two dates and times for equal
  12. Compare two Date