排序日期和时间值 [英] Sort date and time values
问题描述
简单地说,我有一个不同日期和时间的字符串数组.每个元素都具有以下格式:
Putting simply, I have a string array of different dates and times. Each element is of this format:
2015-08-27T22:24:31.903
2015-08-27T22:24:31.903
即YYYY-MM-DD T HH:MM:SS.MMM
That is, YYYY-MM-DDTHH:MM:SS.MMM
我正在对它们进行排序.有没有可用的默认方法可以对这种格式进行排序?
I'm sorting them. Is there a default method available to sort this format?
我现在正在做的是在 T 处分割字符串,将日期和时间转换为相应的时间戳,添加它们,然后对时间戳值进行排序.是否有针对这种格式的直接解析器?最好的解决方案是最欢迎的.
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.
推荐答案
我有一个字符串数组.每个元素的格式均为:
2015-08-27T22:24:31.903
.有没有默认的方法可以对这种格式进行排序?
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?
是的.由于ISO字符串的日期字段按大小降序排列,并且字段是固定宽度的,因此您可以直接使用
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?
是的. LocalDateTime.parse(CharSequence text)
:从诸如2007-12-03T10:15:30
的文本字符串中获取LocalDateTime
的实例.
Yes. LocalDateTime.parse(CharSequence text)
: Obtains an instance of LocalDateTime
from a text string such as 2007-12-03T10:15:30
.
这篇关于排序日期和时间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!