user2584237
user2584237

Reputation:

sorting list by util.date field is not properly sorting if day of util.date is single digit

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

Upvotes: 0

Views: 84

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 340118

The Answer by dams is correct.

Also, your parsing pattern of "yyyy-mm-dd" does not match your input strings "1-10-15".

You are working too hard here, using the wrong classes.

You are using the troublesome old legacy date-time classes now supplanted by the java.time classes.

And you are using a date-time class java.util.Date to represent a date-only value. The time-of-day 00:00:00 is being set implicitly in your Date object, to the UTC time zone. And you use variables named “datetime” that in fact are meant to hold date-only values (confusing and misleading).

Sometimes you use strings that comply with the ISO 8601 standard (YYYY-MM-DD) and sometimes you do not. I suggest you stick with the standard format strictly.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

The java.time classes use ISO 8601 formats by default. So no need to specify a formatting pattern. So you can drop your formatter object and all the calls to formatter.parse.

LocalDate ld = LocalDate.parse( "2015-06-29" );

The LocalDate class implements Comparable and the compareTo method.

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Upvotes: 1

Dams
Dams

Reputation: 997

Try to extract the sort method of the loop !

Collections.sort(list);
for(SortingDate sortingDate:list){
    System.out.println(sortingDate.getStartDateTime().toString());
}

And comment of @assylias is true !

mm is for minutes MM is for month

You should use pattern "yyyy-MM-dd" for the formatter

Upvotes: 3

Related Questions