Java – how do I get the start and end ranges from the timestamp list?

I have such a timestamp string:

2016-01-14T22:43:55Z
2016-01-15T00:04:50Z
2016-01-15T00:44:59+08:30
2016-01-15T01:25:35-05:00
2016-01-15T01:44:31+08:30
2016-01-15T02:22:45-05:00
2016-01-15T02:54:18-05:00
2016-01-15T03:53:26-05:00
2016-01-15T04:32:24-08:00
2016-01-15T06:31:32Z
2016-01-15T07:06:07-05:00

I want to sort them so that I can get the start range and end range from the timestamp above I am doing the following:

List<String> timestamp = new ArrayList<>();
// adding above string timestamp into this list
// Now sort it
Collections.sort(timestamp);

This will give the start and end ranges from the timestamp list above Is this the right way or is there a better way?

timestamp.get(0); // start range
timestamp.get(timestamp.size() - 1); // end range

to update

So I should do the following:

List<OffsetDateTime> timestamp = new ArrayList<>();
timestamp.add(OffsetDateTime.parse( "2016-01-15T00:44:59+08:30" ));
// add other timestamp string like above and then sort it
Collections.sort(timestamp);

timestamp.get(0); // start range
timestamp.get(timestamp.size() - 1); // end range

Solution

OffsetDateTime

Parse these ISO 8601 strings into Java time. Offsetdatetime object

OffsetDateTime.parse( "2016-01-15T00:44:59+08:30" )

Add these date - time objects to the collection and sort them You may need a listlist, such as ArrayList or sortedset

java. The time class implements the CompareTo method to perform the contract in the form of comparable So these objects know how to sort

like this:

List<OffsetDateTime> odts = new ArrayList<>();

OffsetDateTime odt = OffsetDateTime.parse( "2016-01-15T00:44:59+08:30" ) ;
odts.add( odt );
… // Parse remaining ISO 8601 strings,adding each new OffsetDateTime object to collection.

Collections.sort( odts );
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>