如何使用android中的集合从数组列表、两个日期(开始、结束)之间的数据中获取值?

如何使用android中的集合从数组列表、两个日期(开始、结束)之间的数据中获取值?,android,sorting,arraylist,collections,Android,Sorting,Arraylist,Collections,我在arraylist中按日期升序使用以下代码,效果很好。但我需要修改该代码以从arraylist中获取开始日期和结束日期之间的数据 我如何根据我的要求修改它 升序代码(正常工作) Collections.sort(adapter.modelValues,新的Comparator(){ @凌驾 公共整数比较(演员左、演员右){ 日期d1=null,d2=null; 试一试{ SimpleDataFormat f=新的SimpleDataFormat(“dd-MMM-yyyy”,Locale.EN

我在arraylist中按日期升序使用以下代码,效果很好。但我需要修改该代码以从arraylist中获取开始日期和结束日期之间的数据

我如何根据我的要求修改它

升序代码(正常工作)

Collections.sort(adapter.modelValues,新的Comparator(){
@凌驾
公共整数比较(演员左、演员右){
日期d1=null,d2=null;
试一试{
SimpleDataFormat f=新的SimpleDataFormat(“dd-MMM-yyyy”,Locale.ENGLISH);
字符串a=lhs.getlastaction();
字符串b=rhs.getlastaction();
d1=f.parse(a);
d2=f.parse(b);
}捕获(例外e){
Toast.makeText(getActivity().getApplicationContext(),String.valueOf(e.getMessage()),Toast.LENGTH_LONG.show();
}
返回d1。比较(d2);
}
});
代码之间的日期(不工作)

Collections.sort(adapter.modelValues,新的Comparator(){
@凌驾
公共整数比较(演员左、演员右){
日期d1=null,d2=null,d3=null,d4=null;
int-rval=-1;
试一试{
SimpleDataFormat f=新的SimpleDataFormat(“dd-MMM-yyyy”,Locale.ENGLISH);
字符串a=lhs.getlastaction();
字符串b=rhs.getlastaction();
字符串起始日期=“2015年7月28日”;
字符串enddat=“2015年8月21日”;
d1=f.parse(a);
d2=f.parse(b);
d3=f.parse(startdat);
d4=f.parse(enddat);
若(d1.在(d4)之前和(d1.在(d3)之后){
rval=d1。与(d4)相比;
}
}捕获(例外e){
Toast.makeText(getActivity().getApplicationContext(),String.valueOf(e.getMessage()),Toast.LENGTH_LONG.show();
}
返回rval;
}
});

排序后,将在数组列表中显示所有值。但我需要两个日期之间的值(开始、结束)。。如何解决此问题?

compare方法返回两个元素的比较值,使用该值对集合的元素进行排序

要执行所需操作,可以执行以下操作。通过传递集合来创建自己的方法。然后遍历每个元素,如果日期不在两个日期之间,则可以删除(或将预期日期添加到另一个列表中)。

尝试此代码

private ArrayList<Actors> getNewList(ArrayList<Actors> oldList) throws java.text.ParseException {
        ArrayList<Actors> newList = new ArrayList<Actors>();
        Date d1 = null, d2 = null, d3 = null, d4 = null;  
        SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH); 
        String startdat = "28-Jul-2015";
        String enddat = "21-Aug-2015";    
        d3 = f.parse(startdat);
        d4 = f.parse(enddat);   
        for (int i = 0; i < oldList.size(); i++) {

                String b = oldList.get(i).getlastaction();

                d2 = f.parse(b);

                if (d2.compareTo(d3) >= 0 && d2.compareTo(d4) <= 0) {               
                    newList.add(oldList.get(i));
                }
            }     
        return newList;
    }
private ArrayList getNewList(ArrayList oldList)抛出java.text.ParseException{
ArrayList newList=新的ArrayList();
日期d1=null,d2=null,d3=null,d4=null;
SimpleDataFormat f=新的SimpleDataFormat(“dd-MMM-yyyy”,Locale.ENGLISH);
字符串起始日期=“2015年7月28日”;
字符串enddat=“2015年8月21日”;
d3=f.parse(startdat);
d4=f.parse(enddat);
对于(int i=0;i如果(d2.compareTo(d3)>=0&&d2.compareTo(d4),您可以提供一些编码参考吗?使用代码不从Actors获取方法…..字符串a1=oldList.getlastaction();字符串b=oldList.getlastaction();使用oldList.get(index.getlastaction()。有关更多信息,请参阅
  Collections.sort(adapter.modelValues, new Comparator<Actors>() {


                @Override
                public int compare(Actors lhs, Actors rhs) {
                    Date d1 = null, d2 = null, d3 = null, d4 = null;
                    int rval = -1;
                    try {
                        SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);

                        String a = lhs.getlastaction();
                        String b = rhs.getlastaction();
                        String startdat = "28-Jul-2015";
                        String enddat = "21-Aug-2015";
                        d1 = f.parse(a);
                        d2 = f.parse(b);

                        d3 = f.parse(startdat);
                        d4 = f.parse(enddat);

                        if (d1.before(d4) && (d1.after(d3))) {
                            rval = d1.compareTo(d4);
                        }


                    } catch (Exception e) {
                        Toast.makeText(getActivity().getApplicationContext(), String.valueOf(e.getMessage()), Toast.LENGTH_LONG).show();
                    }
                    return rval;

                }
            });
private ArrayList<Actors> getNewList(ArrayList<Actors> oldList) throws java.text.ParseException {
        ArrayList<Actors> newList = new ArrayList<Actors>();
        Date d1 = null, d2 = null, d3 = null, d4 = null;  
        SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH); 
        String startdat = "28-Jul-2015";
        String enddat = "21-Aug-2015";    
        d3 = f.parse(startdat);
        d4 = f.parse(enddat);   
        for (int i = 0; i < oldList.size(); i++) {

                String b = oldList.get(i).getlastaction();

                d2 = f.parse(b);

                if (d2.compareTo(d3) >= 0 && d2.compareTo(d4) <= 0) {               
                    newList.add(oldList.get(i));
                }
            }     
        return newList;
    }