Java – Check given date is in between two given dates or not

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Compare given date is between two gives dates or not
 * 
 * @author Bhavani Polimetla
 * @since 02/10/2014
 * 
 */
public class DateCompareTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		try {
			System.out.println(isDateBetween(SDF.parse("01/10/2014"),
					SDF.parse("01/01/2014"), SDF.parse("01/30/2014")));
			System.out.println(isDateBetween(SDF.parse("02/01/2014"),
					SDF.parse("01/01/2014"), SDF.parse("01/30/2014")));
			System.out.println(isDateBetween(SDF.parse("02/03/2014"),
					SDF.parse("01/27/2014"), SDF.parse("02/14/2014")));

		} catch (ParseException e) {
			e.printStackTrace();
		}

	}

	static final SimpleDateFormat SDF = new SimpleDateFormat("MM/dd/yyyy");

	public static boolean isDateBetween(Date given, Date startDate, Date endDate) {

		boolean status = false;
		if ((given.after(startDate) && (given.before(endDate)))
				|| (SDF.format(given).equals(SDF.format(startDate)) || SDF
						.format(given).equals(SDF.format(endDate)))) {
			status = true;
		}
		System.out.println(status + " given" + SDF.format(given) + " start"
				+ SDF.format(startDate) + " end" + SDF.format(endDate));
		return status;
	}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s