zoukankan      html  css  js  c++  java
  • java中的list时间排序

    最初设想使用:时间long型

        private void testTimes() throws InterruptedException{
            Calendar cal=Calendar.getInstance();
            cal.add(Calendar.DATE, 1);
            cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
            Long time1=cal.getTimeInMillis();
            
            
            Calendar ca2=Calendar.getInstance();
            ca2.add(Calendar.DATE, 1);
            ca2.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
            
            Long time2=ca2.getTimeInMillis();
            
            System.out.println(time1+"--"+time2);
        }

    发现整型可能不一致,即有时候一致,有时候不一致。

    经测算发现,Collections提供了排序功能:

    private void testSort(){
            List<Calendar> cals=new ArrayList<Calendar>();
            
            Calendar cal3=Calendar.getInstance();
            cal3.add(Calendar.DATE, 2);
            cal3.set(cal3.get(Calendar.YEAR), cal3.get(Calendar.MONTH), cal3.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
            
            Calendar cal4=Calendar.getInstance();
            cal4.add(Calendar.DATE, 2);
            cal4.set(cal4.get(Calendar.YEAR), cal4.get(Calendar.MONTH), cal4.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
            
            Calendar cal1=Calendar.getInstance();
            cal1.add(Calendar.DATE, 2);
            cal1.set(cal1.get(Calendar.YEAR), cal1.get(Calendar.MONTH), cal1.get(Calendar.DAY_OF_MONTH), 0, 0, 2);
            
            Calendar cal2=Calendar.getInstance();
            cal2.add(Calendar.DATE, 2);
            cal2.set(cal2.get(Calendar.YEAR), cal2.get(Calendar.MONTH), cal2.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
            
            cals.add(cal3);
            cals.add(cal4);
            cals.add(cal1);
            cals.add(cal2);
            
            Calendar minTime=Collections.min(cals);
    
            System.out.println(DateUtils.formatDate(Collections.min(cals).getTime(),"yyyy-MM-dd HH:mm:ss"));
            
     
            for(int i=0;i<cals.size();i++){
                if(DateUtils.defaultFormatDate(minTime.getTime()).equals(DateUtils.defaultFormatDate(cals.get(i).getTime()))){
                    System.out.println(DateUtils.formatDate(cals.get(i).getTime(),"yyyy-MM-dd HH:mm:ss"));
                }
            }
            /*        for(int i=0;i<cals.size();i++){
                System.out.println(DateUtils.formatDate(cals.get(i).getTime(),"yyyy-MM-dd HH:mm:ss"));
            }
            
            Collections.sort(cals);
            
            //排序后
            for(int i=0;i<cals.size();i++){
                System.out.println(DateUtils.formatDate(cals.get(i).getTime(),"yyyy-MM-dd HH:mm:ss"));
            }*/
        }
  • 相关阅读:
    POJ_1698_Alice's Chance
    透过比特币看移动互联网创业产品立项的关键要素
    localhost与127.0.0.1的区别
    web service初探
    Oracle导出DMP文件的两种方法
    html大小写问题
    浏览器端数据存储
    CSS滚动条样式设置
    浅谈forword和sendRedirect
    Poi对excel的基本操作
  • 原文地址:https://www.cnblogs.com/davidwang456/p/5782054.html
Copyright © 2011-2022 走看看