Finding whether the date is before,after or equal with due date using java

by guruprasad 2013-10-23 14:45:09

To find whether the given date is before,after or equal to the due date:


//setting date format

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

//Assigning variables,

Date testdate=df.parse("2013-10-01");
Date duedate=df.parse("2013-10-23");
Calendar oldCal = Calendar.getInstance();
Calendar newCal = Calendar.getInstance();

oldCal.setTime(testDate);
newCal.setTime(dueDate);

Functions to be used:

To Find whether the date is equal,


if (oldCal.equals(newCal)) {
System.out.println(df.format(testDate) + " and " + df.format(dueDate) + " are equal to each other");
}


To Find whether the date is before,
if (oldCal.before(newCal)) {
System.out.println(df.format(testDate) + " comes before " + df.format(dueDate));

}

To Find whether the date is after,
if (oldCal.after(newCal)) {
System.out.println(df.format(testDate) + " comes after " + df.format(dueDate));
}
512
like
0
dislike
0
mail
flag

You must LOGIN to add comments