按开始和结束时间过滤带有日期的ArrayList

时间:2014-09-27 14:07:48

标签: java android

我正在实现一个简单的ListView,其中数据源是一个ArrayList,其中包含Name,Start和End日期。在iOS中,我会使用一个简单的NSPredicate来过滤数组,但在Android和Java中,我对我应该使用的内容感到困惑。欢迎任何建议。

2 个答案:

答案 0 :(得分:6)

您可以使用Date.before和Date.after方法。这些允许您过滤日期列表(对于特定范围(例如1月)的日期):

一个。使用带有开始日期和结束日期的Java 8过滤器。

湾使用Java循环/迭代器来检查开始和结束日期。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.stream.Collectors;

public class FilterStartAndEndDate {

    private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    private Collection<Date> dateList = null;
    private Date start = null;
    private Date end = null;

    private FilterStartAndEndDate() throws ParseException {
        dateList = new ArrayList<Date>() {{
            add(sdf.parse("01/01/2016"));
            add(sdf.parse("02/01/2016"));
            add(sdf.parse("03/02/2016"));
            add(sdf.parse("04/01/2016"));
            add(sdf.parse("05/01/2016"));
        }};

        start = sdf.parse("31/12/2015");
        end = sdf.parse("01/02/2016");
    }

    /**
     * Filter dates with Lambda
     *
     * @throws ParseException
     */
    private void getDatesBetweenStartAndFinishWithFilter() throws ParseException {
        dateList.stream()
                .filter(dates -> dates.after(start) && dates.before(end))
                .collect(Collectors.toList())
                .forEach(januaryDate->System.out.println(januaryDate));
    }

    /**
     * Filter dates with Iterator
     *
     * @throws ParseException
     */
    private void getDatesBetweenStartAndFinish() throws ParseException {
        Collection<Date> datesInJanuaryList = new ArrayList<>();

        for (Date eachDate : dateList) {
            if (eachDate.after(start) && eachDate.before(end)) {
                datesInJanuaryList.add(eachDate);
            }
        }

        for (Date eachDate : datesInJanuaryList) {
            System.out.println(eachDate);
        }
    }


    public static void main(String[] args) throws Exception {
        FilterStartAndEndDate datesInJanuary = new FilterStartAndEndDate();
        datesInJanuary.getDatesBetweenStartAndFinish();
        datesInJanuary.getDatesBetweenStartAndFinishWithFilter();
    }
}

示例代码过滤器的日期为1月,使用Lambda过滤器和Java迭代器。两者都使用方法之前和之后的日期。

答案 1 :(得分:1)

定义课程

  

包含名称,开始日期和结束日期

定义一个类来保存您的事件名称,开始日期和结束日期。

LocalDate

使用现代的 java.time 类作为日期时间值。 LocalDate类表示没有日期和时区的仅日期值。切勿使用DateCalendarSimpleDateFormat或其他可怕的旧旧日期时间类。

class Event {
    String name;
    LocalDate start, stop;

    // Constructor
    public Event ( String name , LocalDate start , LocalDate stop ) {
        this.name = name;
        this.start = start;
        this.stop = stop;
    }

}

添加一种方法,将传递的LocalDate与开始日期和结束日期进行比较,如果包含在我们的日期范围内,则返回true。通常最好使用半开方法进行比较,其中开始为包含,而结束为排他

    public boolean contains ( LocalDate localDate ) {
        // Regarding the beginning date, a short way of saying2 "is equal to or is later than" is "is not before".
        boolean x = ( ! localDate.isBefore( this.start ) ) && localDate.isBefore( this.stop );
        return x;
    }

建立这些事件的列表。

List< Event > events = new ArrayList<>();
events.add( new Event( "alpha" , LocalDate.of( 2018 , Month.JANUARY , 23 ) , LocalDate.of( 2018 , Month.JANUARY , 28 ) ) );
events.add( new Event( "beta" , LocalDate.of( 2018 , Month.FEBRUARY , 23 ) , LocalDate.of( 2018 , Month.FEBRUARY , 28 ) ) );
events.add( new Event( "gamma" , LocalDate.of( 2018 , Month.MARCH , 23 ) , LocalDate.of( 2018 , Month.MARCH , 28 ) ) );

循环显示这些事件,检查每个事件是否包含我们的目标日期。

LocalDate target = LocalDate.of( 2018 , Month.FEBRUARY , 25 );
List< Event > hits = new ArrayList<>( events.size() );
for ( Event event : events ) {
    if ( event.contains( target ) ) {
        hits.add( event );
    }
}

或者,使用具有lambda语法的Java Streams,而不是for-each循环。效果相同;使用您喜欢的任何语法方法。

LocalDate target = LocalDate.of( 2018 , Month.FEBRUARY , 25 );
List< Event > hits = 
    events
    .stream()
    .filter( event -> event.contains( target ) )
    .collect( Collectors.toList() )
;

为了演示,将所有这些放到一个大类中。我不会在实际工作中嵌套这个Event类。

package com.basilbourque.example;

import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;

public class ListFilterExample {

    public static void main ( String[] args ) {
        ListFilterExample app = new ListFilterExample();
        app.doIt();
    }

    private void doIt () {
        List< Event > events = new ArrayList<>();
        events.add( new Event( "alpha" , LocalDate.of( 2018 , Month.JANUARY , 23 ) , LocalDate.of( 2018 , Month.JANUARY , 28 ) ) );
        events.add( new Event( "beta" , LocalDate.of( 2018 , Month.FEBRUARY , 23 ) , LocalDate.of( 2018 , Month.FEBRUARY , 28 ) ) );
        events.add( new Event( "gamma" , LocalDate.of( 2018 , Month.MARCH , 23 ) , LocalDate.of( 2018 , Month.MARCH , 28 ) ) );

        LocalDate target = LocalDate.of( 2018 , Month.FEBRUARY , 25 );
        List< Event > hits = new ArrayList<>( events.size() );
        for ( Event event : events ) {
            if ( event.contains( target ) ) {
                hits.add( event );
            }
        }
        System.out.println( hits );
    }

    class Event {
        String name;
        LocalDate start, stop;

        public boolean contains ( LocalDate localDate ) {
            // Regarding the beginning date, a short way of saying2 "is equal to or is later than" is "is not before".
            boolean x = ( ! localDate.isBefore( this.start ) ) && localDate.isBefore( this.stop );
            return x;
        }

        // Constructor
        public Event ( String name , LocalDate start , LocalDate stop ) {
            this.name = name;
            this.start = start;
            this.stop = stop;
        }

        @Override
        public String toString () {
            return "Event{ " +
                   "name='" + name + '\'' +
                   "| start=" + start +
                   "| stop=" + stop +
                   " }";
        }
    }
}

运行时。

  

[事件{name ='beta'|开始= 2018-02-23 | stop = 2018-02-28}]

LocalDateRange

如果想花哨的话,请将 ThreeTen-Extra 库(如下所述)添加到您的项目中。使用其LocalDateRange类将您的起止日期对明确地表示为日期范围。该类已经包含一个contains方法,因此无需自己编写。

package com.basilbourque.example;

import org.threeten.extra.LocalDateRange;

import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ListFilterExample {

    public static void main ( String[] args ) {
        ListFilterExample app = new ListFilterExample();
        app.doIt();
    }

    private void doIt () {
        List< Event > events = new ArrayList<>();
        events.add( new Event( "alpha" , LocalDate.of( 2018 , Month.JANUARY , 23 ) , LocalDate.of( 2018 , Month.JANUARY , 28 ) ) );
        events.add( new Event( "beta" , LocalDate.of( 2018 , Month.FEBRUARY , 23 ) , LocalDate.of( 2018 , Month.FEBRUARY , 28 ) ) );
        events.add( new Event( "gamma" , LocalDate.of( 2018 , Month.MARCH , 23 ) , LocalDate.of( 2018 , Month.MARCH , 28 ) ) );

        LocalDate target = LocalDate.of( 2018 , Month.FEBRUARY , 25 );
        // Lambda syntax, instead of for-each loop.
        List< Event > hits = events.stream().filter( event -> event.contains( target ) ).collect( Collectors.toList() );
        System.out.println( hits );
    }

    class Event {
        String name;
        LocalDateRange dateRange;

        public boolean contains ( LocalDate localDate ) {
            // Regarding the beginning date, a short way of saying2 "is equal to or is later than" is "is not before".
            boolean x = this.dateRange.contains( localDate );
            return x;
        }

        // Constructor
        public Event ( String name , LocalDate start , LocalDate stop ) {
            this.name = name;
            this.dateRange = LocalDateRange.of( start , stop );
        }

        @Override
        public String toString () {
            return "Event{ " +
                   "name='" + name + '\'' +
                   "| dateRange=" + dateRange +
                   " }";
        }
    }
}
  

[事件{name ='beta'| dateRange = 2018-02-23 / 2018-02-28}]


关于 java.time

java.time框架已内置在Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendarSimpleDateFormat

目前位于Joda-Timemaintenance mode项目建议迁移到java.time类。

要了解更多信息,请参见Oracle Tutorial。并在Stack Overflow中搜索许多示例和说明。规格为JSR 310

您可以直接与数据库交换 java.time 对象。使用符合JDBC driver或更高版本的JDBC 4.2。不需要字符串,不需要java.sql.*类。

在哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展了java.time。该项目为将来可能在java.time中添加内容提供了一个试验场。您可能会在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore