Sort date and time values

advertisements

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.


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.