2017-03-08 138 views
2

我有一个列表存储Date对象以这种形式:Java - 如何筛选日期列表?

Mon Jan 30 18:00:00 GMT+00:00 2017 
Mon Jan 30 21:00:00 GMT+00:00 2017 
Tue Jan 31 00:00:00 GMT+00:00 2017 
Tue Jan 31 03:00:00 GMT+00:00 2017 
Tue Jan 31 06:00:00 GMT+00:00 2017 

我需要一种方法来过滤我的名单,只保留最后日期的每一天。

这是我的代码的当前状态:

for(int i = 0; i< timeDate.size(); i++){ 

     Calendar calendar = Calendar.getInstance(); 
     calendar.setTime(timeDate.get(i)); 

     int day = calendar.get(Calendar.DAY_OF_MONTH); 
     int month = calendar.get(Calendar.MONTH); 
     int hour = calendar.get(Calendar.HOUR); 

     for(int j=i; j<timeDate.size(); j++){ 
      Date currentDate = timeDate.get(j); 
      calendar.setTime(currentDate); 

      int dayC = calendar.get(Calendar.DAY_OF_MONTH); 
      int monthC = calendar.get(Calendar.MONTH); 
      int hourC = calendar.get(Calendar.HOUR); 

      if(day==dayC && month==monthC && hourC > hour){ 
       timeDate.remove(i); 
      } 
     } 
    } 
+2

日期对象的签名是什么?你的代码的当前状态是什么? –

+4

你到目前为止尝试过什么?创建[mcve]并描述你的问题。 – MBec

+0

刚插入它 –

回答

0

使用java.time

CalendarDate类已经由java取代。时间课程。避免像瘟疫这样的麻烦的传统课程。

转换每个Date你说你给了一个Instant,UTC时间轴上的一个时刻。

Instant instant = myDate.toInstant(); 

收集到List

List<> instants = new ArrayList<>(); 
instants.add(myDate.toInstant()); 
… 
Collections.reverse(instants); // if not already in descending sorted order. 

为在达山应答看出,做一个地图但LocalDateZonedDateTime

我们需要指定一个时区到Instant以获得àZonedDateTime。对于任何特定的时刻,日期因地区而异。

ZoneId z = ZoneId.of("America/Montreal"); 
ZonedDateTime zdt = instant.atZone(); 

提取一个LocalDate

LocalDate ld = zdt.toLocalDate(); 

使用它作为您的键映射。如果没有这样的密钥,请添加它并添加ZonedDateTime作为该Map条目的值。

如果该日期有一个键,检索值ZonedDateTime并进行比较。与isAfter方法比较。如果稍后有一个值,则更换该值。

提示:如果您按照最新的输入排序输入,例如Collections.reverse(instants),您可以优化。如果您找到特定LocalDate的密钥,请不要打电话给isAfter。您知道为该密钥输入的任何值都是最新的。所以继续下一个输入。

您甚至可以通过检查地图来进一步优化。请记住添加到地图的最后一个LocalDate。通过致电isEqual与传入的LocalDate进行比较。如果相等,则转到下一个输入。

3

在这里你去:

public static void main(String[] args){ 
    List<String> list = new ArrayList<>(); 
    SimpleDateFormat dateFomat = new SimpleDateFormat("E MMM dd hh:mm:ss z yyyy"); 
    SimpleDateFormat yyyyMMddFormat = new SimpleDateFormat("yyyyMMdd"); 
    list.add("Mon Jan 30 18:00:00 GMT+00:00 2017"); 
    list.add("Mon Jan 30 21:00:00 GMT+00:00 2017"); 
    list.add("Tue Jan 31 00:00:00 GMT+00:00 2017"); 
    list.add("Tue Jan 31 03:00:00 GMT+00:00 2017"); 
    list.add("Tue Jan 31 06:00:00 GMT+00:00 2017"); 

    Map<String, List<Date>> dateMap = list.stream() 
    .map(s -> { 
     try { 
      return dateFomat.parse(s); 
     } catch (ParseException e) { 
      throw new RuntimeException(e); 
     } 
    }) 
    .collect(Collectors.groupingBy(s -> yyyyMMddFormat.format(s), Collectors.toList())); 

    List<Date> lastDates = new ArrayList<>(); 
    for(Map.Entry<String, List<Date>> entry : dateMap.entrySet()){ 
     lastDates.add(Collections.max(entry.getValue())); 
    } 

    System.out.println(lastDates); 
} 
+3

您可以在'groupingBy'中使用'Collectors.collectingAndThen(maxBy(Comparator.naturalOrder()),Optional :: get)'而不是'Collectors.toList()'作为下游收集器。这种方式不需要额外的迭代。 – eee