jsp文件
<%@taglib uri="/WEB-INF/raqsoftReport.tld" prefix="report"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@include file="/commons/include/html_doctype.html"%> <html> <head> <%@include file="/commons/include/get.jsp"%> <style> .rep { margin: 0 auto; width: 100% } .repWidth { width: 100% } </style> <script> function ChangeValue() { var year = $dp.cal.getDateStr(); $("input[name='year']").val(year); var allWeeks = getNumOfWeeks(year); gencWeeks(allWeeks) } //产生下拉框 function gencWeeks(allWeeks) { //先移除 $("#weeks option").remove(); //在添加 for (var i = 1; i <= allWeeks; i++) { $("#weeks").append("<option value='"+i+"'>" + i + "</option>"); } } //下拉值变化时 function changeWeeks(weekObj) { //alert($(weekObj).val()); var week = $(weekObj).val(); $("input[name='week']").val(week); $("form[name='ghnfgfcForm']").submit(); } /** * 根据年份计算总周数 * @param year * @returns {number} */ function getNumOfWeeks(year) { //设置为这一年开始日期 var startDateOfYear = new Date(year, 0, 1); //计算这一年有多少天 var daysOfYear = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? 366 : 365; //366(365)/7=52.2(52.1),所以一般一年有52周余1天或者2天,当这一年有366天时且第一天是周日,那么他的最后一天则是周一,这一年就有54周。 var weekNum = 53; //当年份是闰年且第一天是周日时有54周 if (startDateOfYear.getDay() == 0 && daysOfYear == 366) { weekNum = 54; } return weekNum; } </script> </head> <body> <form action="${ctx }/platform/common/rep/fgdlZb.htm" method="post" name="ghnfgfcForm"> <input type="hidden" value="${year}" name="year" /> <input type="hidden" value="${week}" name="week" /> <input type="hidden" value="fgdlZb" name="viewName" /> </form> <!-- 顶部按钮 --> <div class="wrapper wrapper-content animated fadeInRight col-sm-12"> <div class="panel-toolbar "> <div class="buttons"> 年份:<input type="text" value="${year}" onclick="WdatePicker({dateFmt: 'yyyy',onpicked:ChangeValue})" /> 第<select id="weeks" onchange="changeWeeks(this)"> <c:forEach var="curWeek" begin="1" step="1" end ="${totleWeekOfYear }"> <option value="${curWeek }" <c:if test='${curWeek==week}'>selected=selected</c:if>>${curWeek}</option> </c:forEach> </select>周 </div> </div> <div class="panel-form col-sm-12"> <div id=reportContainer data-options="region:'center',border:false" style="text-align: center"> <div class="rep repWidth"> <report:html name="fgdlZb" params="year=${year};lastYear=${lastYear };jnzKs='${jnzKs}';jnzJs='${jnzJs}';week=${week }" needPageMark="no" reportFileName="fgdlZb.rpx" needSaveAsWord="yes" needSaveAsExcel="yes" theme="metro" matchScreen="7" /> </div> </div> </div> </div> </body> </html>
后台方法
@RequestMapping("fgdlZb") public ModelAndView fgdlZb(HttpServletRequest request, HttpServletResponse response) throws Exception { String viewName = request.getParameter("viewName"); ModelAndView mav = new ModelAndView(); mav.setViewName("autoView/" + viewName + ".jsp"); String yearStr = request.getParameter("year"); // 下拉框选中第几周 String weekStr = request.getParameter("week"); if(yearStr==null){ Calendar c = Calendar.getInstance(); yearStr=c.get(Calendar.YEAR)+""; int weekOfYear = DateUtil.getWeekOfYear(c.getTime()); //默认显示上一周的 时间 weekStr = (weekOfYear-1)+""; } int year=Integer.parseInt(yearStr); int week=Integer.parseInt(weekStr); int totleWeekOfYear = DateUtil.getMaxWeekNumOfYear(year); Date firstDayOfWeek = DateUtil.getFirstDayOfWeek(year, week-1); Date lastDayOfWeek = DateUtil.getLastDayOfWeek(year, week-1); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); mav.addObject("jnzKs", sdf.format(firstDayOfWeek)); mav.addObject("jnzJs", sdf.format(lastDayOfWeek)); mav.addObject("week", week); mav.addObject("year", year); mav.addObject("lastYear", year-1); mav.addObject("totleWeekOfYear", totleWeekOfYear); return mav; }
附上一个DateUtil工具
package com.ytd.ebos.platform.common.controller; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class DateUtil { /** * 字符串的日期 格式 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(); } }