一,得到三月内的周六,周日
Calendar calendar=Calendar.getInstance();//当前日期 Calendar calendar2=Calendar.getInstance(); int curYear = calendar2.get(Calendar.YEAR); // 得到系统年份 int curMonth = calendar2.get(Calendar.MONTH); // 得到系统月份 int curDay=calendar2.get(Calendar.DAY_OF_MONTH); Calendar nowyear=Calendar.getInstance(); nowyear.set(curYear,curMonth, curDay);//将开始日期设置为当天,如7月17日 Calendar nexty=Calendar.getInstance(); calendar2.add(Calendar.MONTH,4); nexty.set(calendar2.get(Calendar.YEAR),calendar2.get(Calendar.MONTH), 1);//将结束时间设置为三个月后的第一天,如10月1日 calendar.add(Calendar.DAY_OF_MONTH, -calendar.get(Calendar.DAY_OF_WEEK));//周六 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); /** 初始化周六的日期*/ calendar.add(Calendar.DAY_OF_MONTH, -calendar.get(Calendar.DAY_OF_WEEK));//周六 Calendar SaturdayC=(Calendar) calendar.clone(); for(;SaturdayC.before(nexty);SaturdayC.add(Calendar.DAY_OF_YEAR,7)){ if(SaturdayC.after(nowyear)){ Date date = sdf.parse(SaturdayC.get(Calendar.YEAR)+"-"+(1+SaturdayC.get(Calendar.MONTH))+"-"+SaturdayC.get(Calendar.DATE)); String time=sdf.format(date); SaturdayList.add(time); } } /** 初始化周日的日期*/ calendar.add(Calendar.DAY_OF_MONTH,-calendar.get(Calendar.DAY_OF_WEEK)+1);//周日 Calendar SundayC=(Calendar) calendar.clone(); for(;SundayC.before(nexty);SundayC.add(Calendar.DAY_OF_YEAR,7)){ if(SundayC.after(nowyear)){ Date date = sdf.parse(SundayC.get(Calendar.YEAR)+"-"+(1+SundayC.get(Calendar.MONTH))+"-"+SundayC.get(Calendar.DATE)); String time=sdf.format(date); SundayList.add(time); } }
二,得到三个月内的工作日
/** * 得到三个月末的最后一天 */ Calendar cal = Calendar.getInstance(); // 不加下面2行,就是取当前时间前一个月的第一天及最后一天 cal.set(Calendar.YEAR,curYear); cal.set(Calendar.MONTH,curMonth+2); cal.set(Calendar.DAY_OF_MONTH, 1); cal.add(Calendar.DAY_OF_MONTH, -1); Date lastDate = cal.getTime(); String start =curYear+"-"+curMonth+"-"+curDay; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date dBegin = null ; try { dBegin = sdf.parse(start); } catch (ParseException e) { e.printStackTrace(); } WorkdayList.add(sdf.format(dBegin)); Calendar calBegin = Calendar.getInstance(); // 使用给定的 Date 设置此 Calendar 的时间 calBegin.setTime(dBegin); Calendar calEnd = Calendar.getInstance(); // 使用给定的 Date 设置此 Calendar 的时间 calEnd.setTime(lastDate); // 测试此日期是否在指定日期之后 while (lastDate.after(calBegin.getTime())){ // 根据日历的规则,为给定的日历字段添加或减去指定的时间量 calBegin.add(Calendar.DAY_OF_MONTH, 1); WorkdayList.add(sdf.format(calBegin.getTime())); } WorkdayList.removeAll(SaturdayList); WorkdayList.removeAll(SundayList);
三、日期间的比较大小
date1.after(date2) date1.before(date2)
add()是移位,set()是设置
原文地址:http://blog.csdn.net/mingyue_1128/article/details/37905253