I have Two String Datetime as follows:
String Date1 = \"05-09-2013 10:46:10\"
String Date2 = \"06-09-2013 10:46:10\"
I Need to compare thes
Try this,
String pattern = "<yourPattern>";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
try {
Date one = dateFormat.parse(<yourDate>);
Date two = dateFormat.parse(<yourDate>);
}
catch (ParseException e) {}
public static String CompareDates(String date1, String date2) {
try{
String pattern = "dd-MMM-yyyy HH:mm:ss";
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
Date Date1 = formatter.parse(date1);
Date Date2 = formatter.parse(date2);
if (Date1 != null && Date2 != null) {
if (Date1 .equals(Date2 ))
{
//Both dates are equal.
}
else{
//both date are not equal("use after and before to check occurrence of dates")
}
}catch (Exception e1){
e1.printStackTrace();
}
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse("2010-02-22");
Date date2 = sdf.parse("2010-03-23");
int i = date1.compareTo(date2);
switch (i){
case -1: //date1<date2 = -1
return sDate2;
case 1: //date1>date2 = 1
return sDate1;
case 0: //date1==date2= 0
default:
return sDate2;
}
} catch (ParseException e) {
return sDate1;
}