I'm trying to order date objects by date/time (latest first) using code below. The ordering is not correct. I think the compareTo method needs to be implemented differently to achieve the required sorting ?
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class DateSorter {
public static void main(String args[]){
List<DateObject> list = new ArrayList<DateObject>();
DateObject d1 = new DateObject("2012-12-05" , "11:21:19");
list.add(d1);
d1 = new DateObject("2012-12-05" , "11:20:19");
list.add(d1);
d1 = new DateObject("2012-12-05" , "11:20:19");
list.add(d1);
d1 = new DateObject("2012-12-04" , "10:20:19");
list.add(d1);
d1 = new DateObject("2010-12-07" , "13:20:19");
list.add(d1);
d1 = new DateObject("2012-12-05" , "11:20:19");
list.add(d1);
Collections.sort(list);
for(DateObject d : list){
System.out.println(d);
}
}
}
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateObject implements Comparable<Object> {
private String date;
private String time;
public DateObject(String date, String time) {
this.date = date;
this.time = time;
}
public int compareTo(Object o) {
DateFormat formatter;
Date date1 = null;
Date date2 = null;
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
date1 = (Date) formatter.parse(this.date + " " + this.time);
date2 = (Date) formatter.parse(this.date + " " + this.time);
} catch (ParseException e) {
e.printStackTrace();
}
catch(NullPointerException npe){
System.out.println("Exception thrown "+npe.getMessage()+" date1 is "+date1+" date2 is "+date2);
}
return date1.compareTo(date2);
}
@Override
public String toString(){
return this.date+" "+this.time;
}
}
This is the output displayed :
2012-12-05 11:21:19
2012-12-05 11:20:19
2012-12-05 11:20:19
2012-12-04 10:20:19
2010-12-07 13:20:19
2012-12-05 11:20:19
This output should be :
2010-12-07 13:20:19
2012-12-05 11:20:19
2012-12-05 11:20:19
2012-12-05 11:20:19
2012-12-05 11:21:19
2012-12-04 10:20:19
Update : when I modify the compareTo method to :
public int compareTo(Object o) {
DateFormat formatter;
Date date1 = null;
Date date2 = null;
DateObject other = (DateObject) o;
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
date1 = (Date) formatter.parse(this.date + " " + this.time);
date2 = (Date) formatter.parse(other.date + " " + other.time);
} catch (ParseException e) {
e.printStackTrace();
}
catch(NullPointerException npe){
System.out.println("Exception thrown "+npe.getMessage()+" date1 is "+date1+" date2 is "+date2);
}
return date1.compareTo(date2);
}
The output is :
2010-12-07 13:20:19
2012-12-04 10:20:19
2012-12-05 11:20:19
2012-12-05 11:20:19
2012-12-05 11:20:19
2012-12-05 11:21:19
Which is still incorrect as 2012-12-05 should appear before 2012-12-04
You are parsing the same date twice:
date1 = (Date) formatter.parse(this.date + " " + this.time);
date2 = (Date) formatter.parse(this.date + " " + this.time);
should probably be:
date1 = (Date) formatter.parse(this.date + " " + this.time);
DateObject other = (DateObject) o;
date2 = (Date) formatter.parse(other.date + " " + other.time);
and you should probably test that o is a DateObject and not null before trying to cast it and use it.
EDIT
Just tried your updated code using return date2.compareTo(date1); and I get:
2012-12-05 11:21:19
2012-12-05 11:20:19
2012-12-05 11:20:19
2012-12-05 11:20:19
2012-12-04 10:20:19
2010-12-07 13:20:19
which is in descending order (note that the last date is in 2010).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With