JAVA 日期 一个时间段总共几周,每一天星期几
/** * 查询一个时间段的总周数和查询当前时间是第几周 * @param start * @param end * @return */ private int[] selectWeekNum(Date start,Date end,int week,int flag){ java.util.Calendar now = java.util.Calendar.getInstance(); java.util.Calendar c_total = java.util.Calendar.getInstance(); java.util.Calendar c_begin = java.util.Calendar.getInstance(); java.util.Calendar c_end = java.util.Calendar.getInstance(); int count = 0; int weekTotal = 0; try { c_begin.setTime(start); c_end.setTime(end); now.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(sdf.format(new Date()))); int begin = c_begin.get(java.util.Calendar.WEEK_OF_YEAR); int over =c_end.get( java.util.Calendar.WEEK_OF_YEAR); int z = now.get( java.util.Calendar.WEEK_OF_YEAR); if (c_begin.getWeekYear() != c_end.getWeekYear()) { Date totalYear = new SimpleDateFormat("yyyy-MM-dd").parse(c_begin.getWeekYear() + "-12-31"); c_total.setTime(totalYear); int x = c_total.get(java.util.Calendar.WEEK_OF_YEAR); while(x==1){ c_total.add(java.util.Calendar.DAY_OF_MONTH, -1); x = c_total.get(java.util.Calendar.WEEK_OF_YEAR); } //如果结束时间刚好是星期天则不加1 int weekNum = this.dayForWeek(new SimpleDateFormat("yyyy-MM-dd").format(end)); int duoyu = 0; if(weekNum==7) { duoyu = x - begin+1; }else{ duoyu = x - begin + 1+1; } weekTotal = over + duoyu; } else { //如果结束时间刚好是星期天则不加1 int weekNum = this.dayForWeek(new SimpleDateFormat("yyyy-MM-dd").format(end)); if(weekNum==7){ weekTotal = over - begin ; }else{ weekTotal = over - begin + 1; } } if (c_begin.getWeekYear() != now.getWeekYear()) { //计算总周数 int x = c_total.get(java.util.Calendar.WEEK_OF_YEAR); while(x==1){ c_total.add(java.util.Calendar.DAY_OF_MONTH, -1); x = c_total.get(java.util.Calendar.WEEK_OF_YEAR); } int weekNum = this.dayForWeek(new SimpleDateFormat("yyyy-MM-dd").format(start)); int duoyu =0; if(weekNum==7){ duoyu = x - begin + 1+1; }else{ duoyu = x - begin + 1; } count = duoyu + z; } else { int weekNum = this.dayForWeek(new SimpleDateFormat("yyyy-MM-dd").format(start)); if(weekNum==7){ count = (z - begin + 1)+1; }else{ count = (z - begin + 1); } } if (c_end.getTime().getTime() < now.getTime().getTime() || count < 0) { //如果当前时间大于学期结束时间,默然显示第一周 count = 1; }
//下拉框默认显示的东西可根据实际情况删除或保留 if (week != 0 && flag == 1) { count = week; } } catch (Exception e) { e.printStackTrace(); } return new int[]{count,weekTotal}; }
/** * 判断当前日期是星期几 * * @param pTime 修要判断的时间 * @return dayForWeek 判断结果 * @Exception 发生异常 */ public int dayForWeek(String pTime) throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); java.util. Calendar c = java.util. Calendar.getInstance(); c.setTime(format.parse(pTime)); int dayForWeek = 0; if(c.get( java.util. Calendar.DAY_OF_WEEK) == 1){ dayForWeek = 7; }else{ dayForWeek = c.get( java.util. Calendar.DAY_OF_WEEK) - 1; } return dayForWeek; }
/** * 查询一周的开始时间和结束时间 * @param date * @return */ private Map<String,Date> onceWeek(Date date){ java.util.Calendar currentDate = new GregorianCalendar(); currentDate.setTime(date);
currentDate.setFirstDayOfWeek( java.util.Calendar.MONDAY);
currentDate.set( java.util.Calendar.HOUR_OF_DAY, 0);
currentDate.set( java.util.Calendar.MINUTE, 0);
currentDate.set( java.util.Calendar.SECOND, 0);
currentDate.set( java.util.Calendar.DAY_OF_WEEK, java.util.Calendar.MONDAY);
Date dBegin = (Date) currentDate.getTime().clone();
currentDate.setFirstDayOfWeek( java.util.Calendar.MONDAY);
currentDate.set( java.util.Calendar.HOUR_OF_DAY, 23);
currentDate.set( java.util.Calendar.MINUTE, 59);
currentDate.set( java.util.Calendar.SECOND, 59);
currentDate.set( java.util.Calendar.DAY_OF_WEEK, java.util.Calendar.SUNDAY);
Date dEnd =(Date) currentDate.getTime().clone();
//返回星期天和一个星期的日期和周几
Map<String,Date> map = new HashMap<>();
map.put("dBegin",dBegin);
map.put("dEnd",dEnd);
return map;
}
/** * 查询一周的时间 * @param start * @param end * @param week * @param flag * @return */ private List< List<ScheduleVO> > onceWeekList(Date start ,Date end,int week,int flag) { List< List<ScheduleVO>> list = new ArrayList<>(); try { int[] total = this.selectWeekNum(start, end,week,flag); int weekTotal = total[1]; java.util.Calendar currentDate = new GregorianCalendar(); currentDate.setTime(start); for (int i = 0; i < weekTotal; i++) { Map<String,Date> map = this.onceWeek(currentDate.getTime()); List<ScheduleVO> lDate = findDates(map.get("dBegin"),map.get("dEnd")); currentDate.setTime(map.get("dEnd")); currentDate.add(java.util.Calendar.DAY_OF_MONTH, 1); list.add(lDate); } }catch (Exception e){ System.out.print(e.getMessage()); } return list; }
/** *查询当前日期和指定日期为星期几 * @param beginDate * @param endDate * @return */ public static List<ScheduleVO> findDates(Date beginDate, Date endDate) { //数字转中文 SimpleDateFormat sif = new SimpleDateFormat("yyyy-MM-dd"); Map<Integer,String> weekNumString = new HashMap<>(); weekNumString.put(1,"星期一"); weekNumString.put(2,"星期二"); weekNumString.put(3,"星期三"); weekNumString.put(4,"星期四"); weekNumString.put(5,"星期五"); weekNumString.put(6,"星期六"); weekNumString.put(7,"星期天"); ScheduleVO schedule1 = new ScheduleVO(); List<ScheduleVO> lDate = new ArrayList<>(); schedule1.setDate(sif.format(beginDate)); schedule1.setWeekName("星期一"); schedule1.setWeek(1); lDate.add(schedule1); java.util.Calendar cal = java.util.Calendar.getInstance(); cal.setTime(beginDate); boolean bContinue = true; int week = 1; while (bContinue) { //根据日历的规则,为给定的日历字段添加或减去指定的时间量 cal.add(java.util.Calendar.DAY_OF_MONTH, 1); // 测试此日期是否在指定日期之后 if (endDate.after(cal.getTime())) { week++; ScheduleVO schedule = new ScheduleVO(); schedule.setDate(sif.format(cal.getTime())); schedule.setWeekName(weekNumString.get(week)); schedule.setWeek(week); lDate.add(schedule); }else { bContinue = false; } } return lDate; }
//总共有几周 当天是这段时间的第几周
public static Long[] weekNum(String begin,String end) throws ParseException { Calendar cal = Calendar.getInstance(); cal.setTime(DateUtils.parseDate(begin,DateUtils.FORMAT_DATE)); int monday = cal.get(Calendar.DAY_OF_WEEK) - 1; if(monday!=1){ cal.set(cal.DAY_OF_WEEK, cal.MONDAY); begin= DateUtils.format(cal.getTime(),DateUtils.FORMAT_DATE); } System.out.println("学期开始时间所在周周一日期:"+begin); Calendar cal1 = Calendar.getInstance(); cal1.setTime(DateUtils.parseDate(end,DateUtils.FORMAT_DATE)); int sunday = cal1.get(Calendar.DAY_OF_WEEK) - 1; if(sunday!=0){ cal1.set(Calendar.DATE, cal1.get(cal1.DATE) + 6); end = DateUtils.format(cal1.getTime(),DateUtils.FORMAT_DATE); } System.out.println("学期结束时间所在周周日日期:"+end); Long totalWeek = 0l; Long thisWeek = 0l; String nowDate = DateUtils.format(new Date(),DateUtils.FORMAT_DATE); int ss = 1000; int mi = ss * 60; int hh = mi * 60; int dd = hh * 24; Long beginTime =DateUtils.parse(begin,DateUtils.FORMAT_DATE).getTime(); Long endTime =DateUtils.parse(end,DateUtils.FORMAT_DATE).getTime(); //当前时间的星期天 Calendar cal2 = Calendar.getInstance(); cal2.setTime(new Date()); int thisSunDay = cal2.get(Calendar.DAY_OF_WEEK) - 1; if(thisSunDay!=0){ cal2.set(Calendar.DATE, cal2.get(cal2.DATE) + 6); nowDate = DateUtils.format(cal2.getTime(),DateUtils.FORMAT_DATE); } System.out.println("当前时间所在周周日日期:"+nowDate); Long now = DateUtils.parse(nowDate,DateUtils.FORMAT_DATE).getTime(); //得到总共的天数,因为开始时间没有计算进去故加1 Long totalWeekTime = (endTime-beginTime)/ dd+1; Long thisWeekTime = (now-beginTime)/dd+1; totalWeek = totalWeekTime/7;//得到总的周数 if(now>endTime){ thisWeek = 1l;//默认显示第一周 }else if(monday==0){ thisWeek= thisWeekTime/7+1;//因英文第一天是周日故+1 }else{ thisWeek= thisWeekTime/7; } return new Long[]{thisWeek,totalWeek}; }