日期操作相關

zenzuguo發表於2006-10-27
public boolean checkCourseTimes(List schedules, Long schoolId){
boolean flag=false;
if(schedules==null)return false;
schedules.remove(null);
Collections.sort(schedules, new Comparator() {
public int compare(Object a, Object b) {
Schedule cs1 = (Schedule) a;
Schedule cs2 = (Schedule) b;
if(cs1==null||cs2==null)return 0;
Date cs1date=cs1.getStartTime();
Date cs2date=cs2.getStartTime();
int temp=cs1date.compareTo(cs2date);
return temp;
}
});
//firt day
Schedule firtsch=(Schedule)schedules.get(0);
Date firstday=firtsch.getStartTime();
Calendar ca=Calendar.getInstance();
ca.setTime(firstday);
int step = ca.get(Calendar.DAY_OF_WEEK);
ca.add(Calendar.DATE, -step);
Date weekFirstDay = ca.getTime();
//last day
Schedule lastsch=(Schedule)schedules.get(0);
Date lastday=firtsch.getStartTime();
ca.setTime(lastday);
step = 7 - ca.get(Calendar.DAY_OF_WEEK);
ca.add(Calendar.DATE, step);
Date weekLastDay = ca.getTime();
long subs = weekLastDay.getTime()-weekFirstDay.getTime();
long betweenDays = (long)(subs / (1000 * 60 * 60 *24) + 0.5);
int weeks=(int)(betweenDays / 7+0.5);
return flag;
}[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/106285/viewspace-874024/,如需轉載,請註明出處,否則將追究法律責任。

相關文章