below is my pojo class
import java.util.Date;
public class SortingDate implements Comparable<SortingDate> {
private Date date;
private String type;
private String motion;
private Date startDateTime;
private Date endDateTime;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getMotion() {
return motion;
}
public void setMotion(String motion) {
this.motion = motion;
}
public Date getStartDateTime() {
return startDateTime;
}
public void setStartDateTime(Date startDateTime) {
this.startDateTime = startDateTime;
}
public Date getEndDateTime() {
return endDateTime;
}
public void setEndDateTime(Date endDateTime) {
this.endDateTime = endDateTime;
}
@Override
public int compareTo(SortingDate o) {
return getStartDateTime().compareTo(o.getStartDateTime());
}
}
i am trying to sort by StartDateTime field in ascending order. when i try to put sample date ex:2015-07-01 it is not sorting this instance if my date is double digit it is sorting correctly.
My implementation follows:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class SortingDateImpl {
public static void main(String[] args) {
DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
List<SortingDate> list = new ArrayList<>();
try{
SortingDate MO1 = addInstance("1-10-15","DAY","AREWR-DEP",formatter.parse("2015-06-10"),formatter.parse("2015-06-11"));
SortingDate MO21 = addInstance("1-10-15","DAY","ARERR",formatter.parse("2015-06-13"),formatter.parse("2015-06-11"));
SortingDate MO22 = addInstance("1-10-15","Evening","DSAEP",formatter.parse("2015-06-14"),formatter.parse("2015-06-11"));
SortingDate MO31 = addInstance("1-10-15","Evening","AASDRR",formatter.parse("2015-06-23"),formatter.parse("2015-06-11"));
SortingDate MO32 = addInstance("1-10-15","Night","AADSDRR",formatter.parse("2015-06-15"),formatter.parse("2015-06-11"));
SortingDate MO33 = addInstance("1-10-16","DAY","ARASDFR",formatter.parse("2015-06-19"),formatter.parse("2015-06-11"));
SortingDate MO34 = addInstance("1-10-16","Evening","DEADSEP",formatter.parse("2015-07-01"),formatter.parse("2015-06-11"));
SortingDate MO41 = addInstance("1-10-16","Night","AGHGFDSR",formatter.parse("2015-06-24"),formatter.parse("2015-06-11"));
SortingDate MO42 = addInstance("1-10-16","DAY","ARWERR",formatter.parse("2015-06-29"),formatter.parse("2015-06-11"));
SortingDate MO43 = addInstance("1-10-16","Evening","DDFGP",formatter.parse("2015-06-23"),formatter.parse("2015-06-11"));
SortingDate MO51 = addInstance("1-10-15","DAY","ARRER",formatter.parse("2015-06-11"),formatter.parse("2015-06-11"));
SortingDate MO52= addInstance("1-10-15","Evening","ARERR",formatter.parse("2015-06-17"),formatter.parse("2015-06-11"));
SortingDate MO53 = addInstance("1-10-15","Night","ARRYTR",formatter.parse("2015-06-30"),formatter.parse("2015-06-11"));
SortingDate MO54 = addInstance("1-10-16","DAY","ARYRRR",formatter.parse("2015-06-26"),formatter.parse("2015-06-11"));
list.add(MO1);
list.add(MO21);
list.add(MO22);
list.add(MO31);
list.add(MO32);
list.add(MO33);
list.add(MO34);
list.add(MO41);
list.add(MO42);
list.add(MO43);
list.add(MO51);
list.add(MO52);
list.add(MO53);
list.add(MO54);
for(SortingDate sortingDate:list){
Collections.sort(list);
System.out.println(sortingDate.getStartDateTime().toString());
}
}catch(ParseException pe){
pe.printStackTrace();
}
}
private static SortingDate addInstance(String mDate,String mType,String mPortion,Date startdatetime,Date enddatetime) throws ParseException{
DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
SortingDate workUnitBoxInstance = new SortingDate();
workUnitBoxInstance.setDate(formatter.parse(mDate));
workUnitBoxInstance.setType("DAY");
workUnitBoxInstance.setMotion(mPortion);
workUnitBoxInstance.setStartDateTime(startdatetime);
workUnitBoxInstance.setEndDateTime(enddatetime);
return workUnitBoxInstance;
}
}
Output:
Sat Jan 10 00:06:00 IST 2015
Sat Jan 10 00:06:00 IST 2015
Sun Jan 11 00:06:00 IST 2015
Tue Jan 13 00:06:00 IST 2015
Wed Jan 14 00:06:00 IST 2015
Thu Jan 15 00:06:00 IST 2015
Sat Jan 17 00:06:00 IST 2015
Mon Jan 19 00:06:00 IST 2015
Fri Jan 23 00:06:00 IST 2015
Fri Jan 23 00:06:00 IST 2015
Sat Jan 24 00:06:00 IST 2015
Mon Jan 26 00:06:00 IST 2015
Thu Jan 29 00:06:00 IST 2015
Fri Jan 30 00:06:00 IST 2015