Collect Java using sorting by date with same date with AM / PM

I have a date 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 by the below output: (Same date needs to be sorted 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 code but didn't get the 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:

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);


}

      

+3


source to share


2 answers


The problem is what you are using HH

in yours SimpleDateFormat

where you should be using HH

. Characters HH

expect a 24-hour clock string (00-23) and the AM / PM indicator will be ignored.



+4


source


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

I would recommend splitting these lines into a more appropriate datatype - I would use LocalDateTime

in java.time

or Joda Time if possible. At this point (i.e. when you have List<LocalDateTime>

or something like that), just a simple sort will go right.

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



Now that you've posted the code you're using for the comparison, you'll still have the same problem if you've used that code to convert strings to values LocalDateTime

- because you're using HH

in instead of syntax code instead HH

. The format HH

specifier is for 24-hour values ​​and is rarely used in conjunction with the a

(AM / PM specifier). Instead, you should use HH

one that is in the range 01-12.

As a stand-alone aspect, it is very strange to have a property where the setter takes on one type, but the getter returns something completely different ( String

and Date

in your case, accordingly). I would advise you to be consistent.

+2


source







All Articles