Sort date and time values

2019-09-09 21:13发布

Putting simply, I have a string array of different dates and times. Each element is of this format:

2015-08-27T22:24:31.903

That is, YYYY-MM-DDTHH:MM:SS.MMM

I'm sorting them. Is there a default method available to sort this format?

What I'm doing now is splitting the string at T, converting the date and time to respective timestamps, adding them and then sorting the timestamp values. Is there a direct parser for this format available? A better solution is most-welcome.

3条回答
闹够了就滚
2楼-- · 2019-09-09 21:54

I have a string array. Each element is of this format: 2015-08-27T22:24:31.903. Is there a default method available to sort this format?

Yes. Since the ISO string has date fields in descending order of magnitude, and fields are fixed-width, you can simple sort the strings directly using Arrays.sort(Object[] a): Sorts the specified array of objects into ascending order, according to the natural ordering of its elements.

Is there a direct parser for this format available?

Yes. LocalDateTime.parse(CharSequence text): Obtains an instance of LocalDateTime from a text string such as 2007-12-03T10:15:30.

查看更多
手持菜刀,她持情操
3楼-- · 2019-09-09 21:57

I take it those are strings. If you don't want to do the obvious (convert them to java.util.Date or java.time.Instant and compare the result), you can just compare them as strings, that particular datetime format (a nearly-full ISO-8601 timestamp, just missing a timezone indicator) works correctly when you use string comparison.

Gratuitous example (live copy):

import java.util.*;

class Example
{
    public static void main (String[] args)
    {
        String[] strings = {
            "2002-10-24T13:51:25.417",
            "2001-03-25T23:41:24.234",
            "2008-10-05T04:41:56.004",
            "2013-04-10T22:14:06.852",
            "2005-08-24T05:05:01.080",
            "2015-11-20T17:32:27.303",
            "2003-07-16T06:32:07.703",
            "2005-03-20T08:28:18.440",
            "2009-04-17T13:20:09.499",
            "2002-11-04T06:39:55.287"
        };
        Arrays.sort(strings);
        for (String s : strings) {
            System.out.println(s);
        }
    }
}

Output:

2001-03-25T23:41:24.234
2002-10-24T13:51:25.417
2002-11-04T06:39:55.287
2003-07-16T06:32:07.703
2005-03-20T08:28:18.440
2005-08-24T05:05:01.080
2008-10-05T04:41:56.004
2009-04-17T13:20:09.499
2013-04-10T22:14:06.852
2015-11-20T17:32:27.303

You can, of course, provide a Comparator if you need a different ordering (such as reverse order).

查看更多
Luminary・发光体
4楼-- · 2019-09-09 22:10

1) Parse your strings using the appropriate SimpleDateFormat:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss.SSS");
try {
    Date date = formatter.parse(dateInString);
} catch (ParseException e) {
    e.printStackTrace();
}

2) Store each date in a list. To do that firstly instantiate a new list...

ArrayList<Date> myDates = new ArrayList<Date>();

... after the parse command add each date to the list:

myDates.add(date);

3) Sort them using:

Collections.sort(myDates); // Ascending

or

Collections.sort(myDates, Collections.reverseOrder()); // Descending
查看更多
登录 后发表回答