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]
# 1

> 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 :).

kevjavaa at 2007-7-12 18:55:04 > top of Java-index,Java Essentials,Java Programming...
# 2
If you still can't figure it out, try this:[url http://java.sun.com/javase/6/docs/api/java/util/Date.html#compareTo(java.util.Date)]Comparing Dates[/url]
robtafta at 2007-7-12 18:55:04 > top of Java-index,Java Essentials,Java Programming...
# 3

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));

}

}

}

r7r7r7a at 2007-7-12 18:55:04 > top of Java-index,Java Essentials,Java Programming...
# 4
Are you soring dates as Strings? Why not as java.util.Date? then they would be Comparable!
Hippolytea at 2007-7-12 18:55:04 > top of Java-index,Java Essentials,Java Programming...
# 5
> Are you soring dates as Strings? Why not as> java.util.Date? then they would be Comparable!It wouldn't require six levels of indentation, so it wouldn't look nearly as cool :-P.
kevjavaa at 2007-7-12 18:55:04 > top of Java-index,Java Essentials,Java Programming...
# 6

Dude, if getDate returns a java.util.Date object: public class DateComparator implements Comparator < Person > {

public int compare(Person p1, Person p2) {

return p1.getDate().compareTo(p2.getDate());

}

}

Hippolytea at 2007-7-12 18:55:04 > top of Java-index,Java Essentials,Java Programming...
# 7
getDate() returns String format,its not return Dateformat
r7r7r7a at 2007-7-12 18:55:04 > top of Java-index,Java Essentials,Java Programming...
# 8
> getDate() returns String format,its not return> DateformatBut getDate should return a java.util.Date. The heavens will align and great things will happen.
Hippolytea at 2007-7-12 18:55:04 > top of Java-index,Java Essentials,Java Programming...
# 9
yes, but at this line of code:endDate = df.parse(date);you do have a date that you can use. Why do you convert it back to a String?
robtafta at 2007-7-12 18:55:04 > top of Java-index,Java Essentials,Java Programming...
# 10
I think 'D' is for denial. Woops, wrong thread. Sorry.
Hippolytea at 2007-7-12 18:55:04 > top of Java-index,Java Essentials,Java Programming...