Reputation: 150
I have a List of dates in dd.MM.YYYY HH:mm format stored as a string When I try to sort these dates I get all the values sorted properly except for the 00 hours and 12 hours values
The array comes in a 15 minute interval so after sorting i get something like
00:15, 12:15, 00:30, 12:30, 00:45, 12:45, 1:00, 1:15, 1:30....
Below is the code for the sorting
Collections.sort(dates, new Comparator<String>() {
DateFormat f = new SimpleDateFormat("dd.MM.yyyy hh:mm");
@Override
public int compare(String o1, String o2) {
try {
return f.parse(o1).compareTo(f.parse(o2));
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}
});
Please help me out here how do i go ahead with this
Upvotes: 1
Views: 472
Reputation: 97202
In your SimpleDateFormat
, use HH
for hours instead of hh
:
DateFormat f = new SimpleDateFormat("dd.MM.yyyy HH:mm");
As mentioned in the documentation:
H Hour in day (0-23)
h Hour in am/pm (1-12)
Upvotes: 2