Doesn't work
In the below comparator is not working?
p1.getDate() this one returns a String format like 12241998 and i converted into dateformat after that i compared?but its not working plz help me?is possible to compare the dates Using compareTo method?
if there is any wrong in this code plz help me?
package modal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
publicclass dateComparatorimplements Comparator{
publicint compare(Object o1, Object o2){
Person p1 = (Person) o1;
Person p2 = (Person) o2;
String newDate1;
String newDate2;
//iam getting only string format from the externalsystem
//thats y iam conversting into dateformat
newDate1 = convertStringToDate(p1.getDate());
newDate2 = convertStringToDate(p2.getDate());
return newDate1.compareTo(newDate2);
}
/**
* Converting String into dateformat(MM/dd/yyyy).
*/
publicstatic String convertStringToDate(String date){
String billCycleEDate =null;
SimpleDateFormat df =new SimpleDateFormat("MMddyyyy");
SimpleDateFormat df1 =new SimpleDateFormat("MM/dd/yyyy");
Date endDate;
try{
endDate = df.parse(date);
billCycleEDate = df1.format(endDate);
return billCycleEDate;
}catch (ParseException e){
e.printStackTrace();
}
return billCycleEDate;
}
}
[2586 byte] By [
r7r7r7a] at [2007-11-27 7:03:47]

> In the below comparator is not working?
> p1.getDate() this one returns a String format like
> 12241998 and i converted into dateformat after that i
> compared?but its not working plz help me?is possible
> to compare the dates Using compareTo method?
> if there is any wrong in this code plz help me?
You tell me.... which is bigger? "12/24/1998", or "06/11/2007"?
Hint: try to be as stupid as the computer that you're trying to program :).
I got it Thankq
package modal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
public class dateComparator implements Comparator {
public int compare(Object o1, Object o2) {
Person p1 = (Person) o1;
Person p2 = (Person) o2;
String newDate1;
String newDate2;
//iam getting only string format from the externalsystem
//thats y iam conversting into dateformat
newDate1 = p1.getDate();
newDate2 = p2.getDate();
System.out.println(newDate1.substring(4,8));
System.out.println(newDate1.substring(2,4));
System.out.println(newDate2.substring(0,2));
if ((newDate1.substring(4,8)).equals(newDate2.substring(4,8)))
{
if((newDate1.substring(2,4)).equals(newDate2.substring(2,4)))
{
if((newDate1.substring(0,2)).equals(newDate2.substring(0,2)))
{
return 0;
}
else
{
return ((newDate1.substring(0,2)).compareTo(newDate2.substring(0,2)));
}
}
else {
return (newDate1.substring(2,4)).compareTo(newDate2.substring(2,4));
}
}
else {
return (newDate1.substring(4,8)).compareTo(newDate2.substring(4,8));
}
}
}