-3

I have a list of dates is yyyy-MM-ddTHH:mm:ss.SSS formate. I want to sort these dates in Ascending and Descending order. Is there any collection method to sort these dates? How can I sort these dates

Thanks in Advance

unflagged.destination
  • 1,576
  • 3
  • 19
  • 38

4 Answers4

2

1) Parse your strings using the appropriate SimpleDateFormat:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss.SSS");
try {
    Date date = formatter.parse(dateInString);
} catch (ParseException e) {
    e.printStackTrace();
}

2) Store each date in a list. To do that firstly instantiate a new list...

ArrayList<Date> myDates = new ArrayList<Date>();

... and secondly, in your try statement, after the parse command add each date to the list:

myDates.add(date);

3) Sort them using:

Collections.sort(myDates); // Ascending

or

Collections.sort(myDates, Collections.reverseOrder()); // Descending

Thanos
  • 3,627
  • 3
  • 24
  • 35
1

To get the milliseconds of the DateTimes I'm using the Joda Time API: http://www.joda.org/joda-time/

public class Testing 
{
    public static void main(String[] args) {
        //yyyy-MM-ddTHH:mm:ss.SSS
        String date1 = "2015-01-10T11:00:00.000";
        String date2 = "2015-01-15T12:00:00.000";
        String date3 = "2015-01-20T13:00:00.000";

        List<String> dates = new ArrayList<String>();

        dates.add(date2);
        dates.add(date1);
        dates.add(date3);

        sort(dates);
        for(String str : dates) {
            System.out.println(str);
        }
    }

    public static void sort(List<String> dates) {
        Collections.sort(dates,new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                //The dates are currently sorted in Ascending order
                return Long.compare(new DateTime(o1).getMillis(), new DateTime(o2).getMillis());
                //To have them in Descending order just switch the DateTimes around
            }
        });
    }
}
Brunaldo
  • 66
  • 6
1

Convert you string dates as datetime:

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-ddTHH:mm:ss.SSS");
DateTime dt = formatter.parseDateTime(string);

Put them in a list.

Then sort the list:

Collections.sort(list);
Heschoon
  • 2,915
  • 9
  • 26
  • 55
0
1)First Iterate the List Of dates in string format .
On Each Item Call GetLongValueOfDateTimeString(date in string format) Then Return Value is in Long 

public long GetLongValueOfDateTimeString(String dateTimeInStringFormat) {
        DateFormat f = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss.SSS");
        Date date;
        long dateinMillis = 0;
        try {
            date = f.parse(dateTimeInStringFormat);
            dateinMillis = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateinMillis;
    }

2) Store each value  in a list of long Value(below is the implementation).

long[] longs = new long[]{1L};
Long[] longObjects = ArrayUtils.toObject(longs);
List<Long> longList = java.util.Arrays.asList(longObjects);
longList.add(dateinMillis);

3) Sort them using:

Collections.sort(longList); (Ascending Order)
Collections.sort(longList).reverse(); (Descending Order)