Link to home
Start Free TrialLog in
Avatar of ldbkutty
ldbkuttyFlag for India

asked on

compare date strings in java

hai,

i have "startDate" and "endDate" and getting them in request.getParameter(). Now i want to compare the date difference between them.

Just want to verfiy if endDate is greater than startDate.
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Giant2
Giant2

:)

yyyy/mm/dd
String format="MM/dd/yyyy"
DateFormat myDateFormat = new SimpleDateFormat(strFormat);
Date startdate= null;
Date enddate=null;
try {
     startdate= myDateFormat.parse(request.getParameter("startdate"));
     enddate= myDateFormat.parse(request.getParameter("enddate")
} catch (ParseException e) {
     System.out.println("Invalid Date Parser Exception ");
     e.printStackTrace();
}
oops already accepted
>> getting them in request.getParameter()
Guess you get them as String then.

String startDate = ...;
String endDate = ...;

SimpleDateFormat fmt = new SimpleDateFormat("yyyy/mm/dd");   // or whatever the format may be
java.util.Date start = fmt.parse(startDate);
java.util.Date end = fmt.parse(endDate);
if ( end.after(start) ) {
   System.out.println("The end date is greater than the start Date");
}
else if ( end.before(start) ) {
   System.out.println("The end date is smaller than the start Date");
}
else {
   System.out.println("The end date equals the start Date");
}
>> oops already accepted
Same feeling ;°)

Thanks for accepting ldbkutty.