开发者

Sorting of Dates with the help for Date formatter

开发者 https://www.devze.com 2023-04-05 06:40 出处:网络
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 Ho

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.

开发者_JS百科

Thanks in advance.


Since you mention in your question that you already have an ArrayList of Strings, 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:

Date and time values are organized from the most to the least significant: year, month (or week), day, hour, minute, second, and fraction of second. The lexicographical order of the representation thus corresponds to chronological order, except for date representations involving negative years. This allows dates to be naturally sorted by, for example, file systems.

In more detail:

List<String> dateStrings = Arrays.asList(
    "2011-07-18T10:39:31.855Z",
    "2012-09-18T10:19:31.855Z",
    "2011-07-18T10:39:31.055Z",
    "1903-12-01T00:39:31Z",
);
Arrays.sort(dateStrings);
// now dateStrings is sorted by date.

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 Date objects, you can also just call Arrays.sort and 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.


Just format all your dates in a format where sorting would make sense: e.g. year first, then 2-digit month, etc. Example format string: "yyyy-MM-dd hh:mm:ss".

Then just sort the strings, e.g. by using a TreeSet.


You can use this type of sort.

 ArrayList arrayList = new ArrayList();

 //Add elements to Arraylist
 arrayList.add("1");   //1->> you can add you data format by function or static
 arrayList.add("3");
 arrayList.add("5");
 arrayList.add("2");
 arrayList.add("4");

/*
 To sort an ArrayList object, use Collection.sort method. This is a
 static method. It sorts an ArrayList object's elements into ascending order.
*/
 Collections.sort(arrayList);

 //display elements of ArrayList
  System.out.println("ArrayList elements after sorting in ascending order : ");
 for(int i=0; i<arrayList.size(); i++)
  System.out.println(arrayList.get(i));

you can also follow the link, which is more helpful you. Link1 , Link 2 and Link 3

0

精彩评论

暂无评论...
验证码 换一张
取 消