How to get start and end range from list of timestamps?

后端 未结 2 1558
甜味超标
甜味超标 2020-11-27 08:53

I have a timestamp string like this:

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
         


        
相关标签:
2条回答
  • 2020-11-27 09:16

    You must use Comparator. First of all create a class and implement Comparator. And then compare value1 and value2 with your choice. After that use this class to Collections.sort.

    Regards, a.ayati

    0 讨论(0)
  • 2020-11-27 09:29

    OffsetDateTime

    Parse those ISO 8601 strings into java.time.OffsetDateTime objects.

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

    Add those date-time objects to a Collection and sort. You probably want a List such as ArrayList or a SortedSet.

    The java.time classes implement the compareTo method, to fulfill their contract as a 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 );
    
    0 讨论(0)
提交回复
热议问题