Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort Date which is in string format in java?

I have an arraylist with string values like

ArrayList<String> datestring=new ArrayList<String>();
datestring.add("01/21/2013 @03:13 PM");
datestring.add("01/21/2013 @04:37 PM");
datestring.add("01/21/2013 @10:41 AM");
datestring.add("01/21/2013 @10:48 AM");
datestring.add("01/22/2013 @06:16 AM");
datestring.add("01/22/2013 @06:19 AM");
datestring.add("01/21/2013 @05:19 PM");
datestring.add("01/21/2013 @05:19 PM");

Can any body help me on sorting the above list? So that the values are sorted according to AM and PM format.
The expected output after sorting should be

for (String s : datestring)
{
    System.out.println(s);
}

.

01/21/2013 @10:41 AM;
01/21/2013 @10:48 AM;
01/21/2013 @03:13 PM;
01/21/2013 @04:37 PM;
01/21/2013 @05:16 PM;
01/21/2013 @05:19 PM;
01/22/2013 @06:16 AM;
01/22/2013 @06:19 AM;
like image 978
user1740005 Avatar asked Jan 22 '13 05:01

user1740005


People also ask

How do I sort dates in string format?

ArrayList<String> datestring=new ArrayList<String>(); datestring. add("01/21/2013 @03:13 PM"); datestring. add("01/21/2013 @04:37 PM"); datestring. add("01/21/2013 @10:41 AM"); datestring.

Can we sort date in Java?

The sort() method that is a part of Comparator mechanism of Collection class that sorts the data in decreasing order. If we want to achieve the aim in generic while considering the boundary condition where the objects to be sorted are user-defined, we can use Comparator interface.

How do I convert a string to a date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.


3 Answers

try this

    Collections.sort(datestring, new Comparator<String>() {
        DateFormat f = new SimpleDateFormat("MM/dd/yyyy '@'hh:mm a");
        @Override
        public int compare(String o1, String o2) {
            try {
                return f.parse(o1).compareTo(f.parse(o2));
            } catch (ParseException e) {
                throw new IllegalArgumentException(e);
            }
        }
    });

or with Java 8

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy '@'hh:mm a");
    Collections.sort(datestring, (s1, s2) -> LocalDateTime.parse(s1, formatter).
            compareTo(LocalDateTime.parse(s2, formatter)));
like image 67
Evgeniy Dorofeev Avatar answered Oct 07 '22 06:10

Evgeniy Dorofeev


One can compare the List of date in string format by using the compareTo as given below.

ArrayList<String> datestring=new ArrayList<String>();
datestring.add("01/21/2013 @03:13 PM");
datestring.add("01/21/2013 @04:37 PM");
datestring.add("01/21/2013 @10:41 AM");
datestring.add("01/21/2013 @10:48 AM");
datestring.add("01/22/2013 @06:16 AM");
datestring.add("01/22/2013 @06:19 AM");
datestring.add("01/21/2013 @05:19 PM");
datestring.add("01/21/2013 @05:19 PM");

Collections.sort(datestring, new Comparator<String>() {
                @Override
                public int compare(String object1, String object2) {
                    return object1.compareTo(object2);
                }
            });

By using this you even dont have to parse the String into date

like image 11
Navankur Chauhan Avatar answered Oct 07 '22 04:10

Navankur Chauhan


One option is to add the dates to a TreeMap, with the unformatted date as the key, and the formatted date as the value. Using a TreeMap will sort the values automatically.

private ArrayList<String> sortDates(ArrayList<String> dates) throws ParseException {
    SimpleDateFormat f = new SimpleDateFormat("dd MMM yyyy");
    Map <Date, String> dateFormatMap = new TreeMap<>();
    for (String date: dates)
        dateFormatMap.put(f.parse(date), date);
    return new ArrayList<>(dateFormatMap.values());
}
like image 7
Conor Pender Avatar answered Oct 07 '22 05:10

Conor Pender