Java Date check if a date is between two date values

Description

Java Date check if a date is between two date values

import java.time.Instant;
import java.util.Calendar;
import java.util.Date;

public class Main {
  public static void main(String[] argv) {
    Date appointDate = new Date();
    appointDate.setTime(10000);/* w ww . j a v  a2s  . c om*/
    Date startDate = Date.from(Instant.EPOCH);
    Date endDate = Date.from(Instant.now());
    System.out.println(checkDateInBetweenDays(appointDate, startDate, endDate));
  }

  public static boolean checkDateInBetweenDays(Date appointDate, Date startDate, Date endDate) {

    if (compareDate(appointDate, endDate) < 0) {
      return false;
    }

    if (compareDate(appointDate, startDate) > 0) {
      return false;
    }
    return true;
  }

  public static long compareDate(Date start, Date end) {
    long temp = 0;
    Calendar starts = Calendar.getInstance();
    Calendar ends = Calendar.getInstance();
    starts.setTime(start);
    ends.setTime(end);
    temp = ends.getTime().getTime() - starts.getTime().getTime();
    return temp;
  }
}



PreviousNext

Related