ldbkutty
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
String format="MM/dd/yyyy"
DateFormat myDateFormat = new SimpleDateFormat(strFormat );
Date startdate= null;
Date enddate=null;
try {
startdate= myDateFormat.parse(request .getParame ter("start date"));
enddate= myDateFormat.parse(request .getParame ter("endda te")
} catch (ParseException e) {
System.out.println("Invali d Date Parser Exception ");
e.printStackTrace();
}
DateFormat myDateFormat = new SimpleDateFormat(strFormat
Date startdate= null;
Date enddate=null;
try {
startdate= myDateFormat.parse(request
enddate= myDateFormat.parse(request
} catch (ParseException e) {
System.out.println("Invali
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");
}
Guess you get them as String then.
String startDate = ...;
String endDate = ...;
SimpleDateFormat fmt = new SimpleDateFormat("yyyy/mm/
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.
Same feeling ;°)
Thanks for accepting ldbkutty.
yyyy/mm/dd