最初设想使用:时间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")); }*/ }
时间: 2024-10-26 01:24:28