timeUtil
/** * 字符串的日期 格式 yyyy-MM-dd 转入参数,添加或减去它的天数,在返回字符串 */ public static String addOrDelRq(String rq, int num) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.setTime(sdf.parse(rq)); c.add(Calendar.DATE, num); String rtn = sdf.format(c.getTime()); return rtn; } // 获取当前时间所在年的周数 public static int getWeekOfYear(Date date) { Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setMinimalDaysInFirstWeek(7); c.setTime(date); return c.get(Calendar.WEEK_OF_YEAR); } // 获取当前时间所在年的最大周数 public static int getMaxWeekNumOfYear(int year) { Calendar c = new GregorianCalendar(); c.set(year, Calendar.DECEMBER, 31, 23, 59, 59); return getWeekOfYear(c.getTime()); } // 获取某年的第几周的开始日期 public static Date getFirstDayOfWeek(int year, int week) { // week+=-1; Calendar c = new GregorianCalendar(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, Calendar.JANUARY); c.set(Calendar.DATE, 1); Calendar cal = (GregorianCalendar) c.clone(); cal.add(Calendar.DATE, week * 7); cal.add(Calendar.DATE, -1); /* * //如果第一周有上一年的时间 if(week==1){ * * } */ return getFirstDayOfWeek(cal.getTime()); } // 获取某年的第几周的结束日期 public static Date getLastDayOfWeek(int year, int week) { // week+=-1; Calendar c = new GregorianCalendar(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, Calendar.JANUARY); c.set(Calendar.DATE, 1); Calendar cal = (GregorianCalendar) c.clone(); cal.add(Calendar.DATE, week * 7); cal.add(Calendar.DATE, -1); return getLastDayOfWeek(cal.getTime()); } // 获取当前时间所在周的开始日期 public static Date getFirstDayOfWeek(Date date) { Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday return c.getTime(); } // 获取当前时间所在周的结束日期 public static Date getLastDayOfWeek(Date date) { Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday return c.getTime(); }