Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Collection Using Date Sorting on Same Date with AM/PM

Tags:

java

date

I have Date as listed below:

17/03/2015 09:38:39 AM
17/03/2015 10:52:26 AM
10/03/2015 08:30:56 AM
02/03/2015 09:18:10 AM
02/03/2015 09:37:23 AM
02/03/2015 11:25:01 AM
02/03/2015 11:29:00 AM
02/03/2015 11:42:38 AM
02/03/2015 12:04:39 PM
02/03/2015 12:09:05 PM
02/03/2015 01:17:09 PM
02/03/2015 01:29:08 PM

I want them to sort them as per below result: (Same date one should be sort by timestamp)

17/03/2015 10:52:26 AM
17/03/2015 09:38:39 AM
10/03/2015 08:30:56 AM
02/03/2015 01:29:08 PM
02/03/2015 01:17:09 PM
02/03/2015 12:09:05 PM
02/03/2015 12:04:39 PM
02/03/2015 11:42:38 AM
02/03/2015 11:29:00 AM
02/03/2015 11:25:01 AM
02/03/2015 09:37:23 AM
02/03/2015 09:18:10 AM

I tried with the code, but not getting desired output:

Collections.sort(listOfDate, new Comparator<SR>() {
  public int compare(SR m1, SR m2) {
    try {
      return m2.getCreatedDateActual().compareTo(m1.getCreatedD ateActual());
    } catch (Exception e) {
    }
    return 0;
  }
});

Output coming is:

17/03/2015 10:52:26 AM
17/03/2015 09:38:39 AM
10/03/2015 08:30:56 AM
02/03/2015 12:09:05 PM
02/03/2015 12:04:39 PM
02/03/2015 11:42:38 AM
02/03/2015 11:29:00 AM
02/03/2015 11:25:01 AM
02/03/2015 09:37:23 AM
02/03/2015 09:18:10 AM
02/03/2015 01:29:08 PM (Getting wrong date here)
02/03/2015 01:17:09 PM (Getting wrong date here)

Here is my SR class.

private String createdDateActual;

public void setCreatedDateActual(String createdDateActual) {
    this.createdDateActual = createdDateActual;
}

public Date getCreatedDateActual() throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss a");

    return sdf.parse(createdDateActual);


}
like image 973
user2936722 Avatar asked Apr 20 '15 06:04

user2936722


People also ask

How to format date with AM PM java?

Approach 1: SimpleDateFormattext. DateFormat class. The format() method of DateFormat class in Java is used to format a given date into Date/Time string. Basically, the method is used to convert this date and time into a particular format i.e., “mm/dd/yyyy”.

Can we sort date in java?

In Java, we have multiple methods to sort objects in ArrayList by Date. This can be done by using the Comparable<> interface or by the Collections. sort() method, to accomplish this task you can use any one of them.


2 Answers

The problem is that you're using HH in your SimpleDateFormat, where you should be using hh. The characters HH expect a 24-hour clock string (00-23), and the AM/PM indicator will be ignored.

like image 142
Dawood ibn Kareem Avatar answered Oct 22 '22 11:10

Dawood ibn Kareem


The core problem IMO is that you don't have a collection of dates - you have a collection of strings.

I would recommend parsing those strings into a more suitable data type - I'd use LocalDateTime in either java.time or Joda Time if you possibly can. At that point (i.e. when you've got a List<LocalDateTime> or something similar), just a simple sort will do the right thing.

Note that it's not just AM/PM that will cause you issues at the moment - "12:05 AM" should come before "01:00 AM" for example. If your strings were in 24 hour format instead of using AM/PM, neither of these would cause a problem - but you'd still be better off having data in a more natural representation.

Now that you've posted the code you're using for comparisons, you'd still have the same problem if you used that code to convert the strings into LocalDateTime values - because you're using HH in the parsing code instead of hh. The HH format specifier is for 24-hour values, and is very rarely appropriate in conjunction with a (the AM/PM specifier). Instead, you should use hh which is in the range 01-12.

As a separate aside, it's very odd to have a property where the set method takes one type, but the get method returns something totally different (String and Date in your case respectively). I'd encourage you to be consistent.

like image 25
Jon Skeet Avatar answered Oct 22 '22 12:10

Jon Skeet