Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday, 11 July 2016

Two date compare in java




private void sorting()
{
    List<Date> dateList = new ArrayList<Date>();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.US);
    try {
        dateList.add(sdf.parse("01/12/2013 04:10:06 AM"));
        dateList.add(sdf.parse("02/11/2013 09:10:10 PM"));
        dateList.add(sdf.parse("03/12/2013 03:10:03 AM"));
        dateList.add(sdf.parse("04/10/2013 04:10:02 PM"));
        dateList.add(sdf.parse("05/05/2013 02:10:09 AM"));
        dateList.add(sdf.parse("06/12/2003 01:10:02 PM"));

        dateList.add(sdf.parse("07/03/2013 04:10:02 PM"));
        dateList.add(sdf.parse("05/01/2013 02:10:09 AM"));
        dateList.add(sdf.parse("07/07/2003 01:10:02 PM"));
        dateList.add(new Date());

        Collections.sort(dateList);
        Comparator<Date> cmp = new Comparator<Date>() {

            @Override
            public int compare(Date o1, Date o2) {
                if(o1.getTime() > o2.getTime()) return 1;
                else if(o1.getTime() < o2.getTime()) return -1;
                else return 0;
            }
        };

        List<Date>  out_of_date_list = new ArrayList<Date>();
        for(Date date : dateList){
            if(cmp.compare(date,new Date())<0){
                out_of_date_list.add(date);
            }
        }

        Log.e("Main"," >>> print all out of date  ");
        Collections.sort(out_of_date_list, Collections.reverseOrder());
        out_of_date_list.remove(0);
        for(Date date : out_of_date_list){

            Log.e("Main"," >>> date = "+sdf.format(date));
        }
    } catch (ParseException e) {// TODO Auto-generated catch block
        e.printStackTrace();
    }
}


Thank you

No comments:

Post a Comment