I have a problem that I want to Sort the String Arraylist of Dates with format= 2011-07-18T10:39:31.855Z with the help of Date formatter in java. But don’t know how to do that? Can any one tell me How to use Date formatter with custom format for sorting of data?
Please help me out about this problem.
Thanks in advance.
Since you mention in your question that you already have an
ArrayListofStrings, and you show that the strings are all dates that are already formatted in ISO8601, your task is very simple: just sort the arraylist as-is.That is one of the many benefits of ISO8601!
From Wikipedia:
In more detail:
Again the idea here is that you do not have to convert the strings to dates to do the sort.
As an aside, if your list contained actual
Dateobjects, you can also just callArrays.sortand your list would get sorted properly as well, because dates are comparable in Java. But your question asked about strings, and strings in ISO8601 at that, so if this was an important part of your problem you might as well take advantage of that benefit of the format. TL;DR you don’t need a dateformat to sort.