I'm lazy, I know (by hey, at least next time I Google it then I'll find the answer)
Do you have an existing piece of code that takes a Date object, and generates a string, that has a sort order equivalent to the chronological order?
I want the string to contain all the date pieces (day, month, hour, minute, milliseconds, ...) and that when two strings are compared, the string representing the earliest date will be before the string representing the later date.
Date[] dates=//....;
List<String> listToSort=new ArrayList<String>(dates.length);
SimpleDateFormat format=new SimpleDateFormat ("yyyyMMddHHmmssSSS");
for(Date date: dates) {
String sDate=format.format(date);
listToSort.add(sDate);
}
Collections.sort(listToSort);
I'm lazy too, so maybe it has some compile errors, or not (haven't checked it).
java.text.DateFormat dateFormat = new java.text.SimpleDateFormat("yyyyMMddHHmmssSSSS"); java.util.Date date = new java.util.Date(); // convert to GMT, if needed, look at this String sortableDate = dateForamt.format(date);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With