Java Data Type How to - Determine if a date is between two dates


We would like to know how to determine if a date is between two dates.


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
//w w  w .j  av a  2  s . com
public class Main {

  public static void main(String[] args) throws ParseException {

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

    String oeStartDateStr = "04/01/2015";
    String oeEndDateStr = "11/14/2015";

    Calendar cal = Calendar.getInstance();
    Integer year = cal.get(Calendar.YEAR);

    oeStartDateStr = oeStartDateStr.concat(year.toString());
    oeEndDateStr = oeEndDateStr.concat(year.toString());

    Date startDate = sdf.parse(oeStartDateStr);
    Date endDate = sdf.parse(oeEndDateStr);
    Date d = new Date();
    String currDt = sdf.format(d);

    if ((d.after(startDate) && (d.before(endDate)))
        || (currDt.equals(sdf.format(startDate)) || currDt.equals(sdf
            .format(endDate)))) {
      System.out.println("Date is between 1st april to 14th nov...");
    } else {
      System.out.println("Date is not between 1st april to 14th nov...");

The code above generates the following result.