java实现本月本周本日
Posted 爱吃苹果的西瓜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java实现本月本周本日相关的知识,希望对你有一定的参考价值。
Controller
1 @ResponseBody 2 @RequestMapping("/feedback/listFeedbacks") 3 public ControllerResult listFeedbacks(String starttime, String endtime, String currenttimetype,String pagenum,String pagesize) throws Exception{ 4 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 5 /* 测试 6 String startTime="2019-01-11 13:13:13"; 7 String endTime="2019-01-02 11:11:11";*/ 8 if(pagenum==null||pagenum==" "){ 9 pagenum="1"; 10 } 11 if(pagesize==null||pagesize==" "){ 12 pagesize="10000000"; 13 } 14 int pN=Integer.parseInt(pagenum); 15 int pS=Integer.parseInt(pagesize); 16 int start=(pN-1)*pS; 17 int end=pN*pS-start; 18 List<Map<String,Object>> feedbacks=new ArrayList<>(); 19 if(currenttimetype==null || currenttimetype=="") { 20 if (starttime == null || starttime == "") { 21 starttime = "2000-01-01 00:00:00"; 22 } 23 if (endtime == null || endtime == "") { 24 endtime = "3000-01-01 00:00:00"; 25 } 26 // Date startData = sdf.parse(starttime); 27 // Date endData = sdf.parse(endtime); 28 feedbacks = feedbackService.listFeedbacks(start,end,starttime,endtime); 29 }else if(currenttimetype.equals("currentDay")){ 30 Calendar calTodayBegin = Calendar.getInstance(); 31 calTodayBegin.set(Calendar.HOUR_OF_DAY, 0); 32 calTodayBegin.set(Calendar.MINUTE, 0); 33 calTodayBegin.set(Calendar.SECOND, 0); 34 calTodayBegin.set(Calendar.MILLISECOND, 0); 35 starttime=sdf.format(calTodayBegin.getTime()); 36 Calendar calTodayEnd = Calendar.getInstance(); 37 calTodayEnd.set(Calendar.HOUR_OF_DAY, 23); 38 calTodayEnd.set(Calendar.MINUTE, 59); 39 calTodayEnd.set(Calendar.SECOND, 59); 40 endtime=sdf.format(calTodayEnd.getTime()); 41 System.out.println(calTodayEnd.getTime()); 42 System.out.println(calTodayBegin.getTime()); 43 System.out.println(starttime); 44 System.out.println(endtime); 45 feedbacks=feedbackService.listFeedbacks(start,end,starttime,endtime); 46 }else if(currenttimetype.equals("currentWeek")){ 47 Date date = new Date(); 48 if (date == null) { 49 return null; 50 } 51 Calendar calWeekBegin = Calendar.getInstance(); 52 calWeekBegin.setTime(date); 53 int dayofweek = calWeekBegin.get(Calendar.DAY_OF_WEEK); 54 if (dayofweek == 1) { 55 dayofweek += 7; 56 } 57 calWeekBegin.add(Calendar.DATE, 2 - dayofweek); 58 calWeekBegin.set(Calendar.HOUR_OF_DAY, 0); 59 calWeekBegin.set(Calendar.MINUTE, 0); 60 calWeekBegin.set(Calendar.SECOND, 0); 61 Date startDate=calWeekBegin.getTime(); 62 starttime=sdf.format(startDate); 63 Calendar calWeekEnd = Calendar.getInstance(); 64 calWeekEnd.setTime(startDate); 65 calWeekEnd.add(Calendar.DAY_OF_WEEK, 6); 66 calWeekEnd.set(Calendar.HOUR_OF_DAY, 23); 67 calWeekEnd.set(Calendar.MINUTE, 59); 68 calWeekEnd.set(Calendar.SECOND, 59); 69 Date endDate = calWeekEnd.getTime(); 70 endtime=sdf.format(endDate); 71 feedbacks=feedbackService.listFeedbacks(start,end,starttime,endtime); 72 }else if(currenttimetype.equals("currentMonth")){ 73 Calendar beginOfCM = Calendar.getInstance(); 74 beginOfCM.add(Calendar.MONTH,0); 75 beginOfCM.set( Calendar.DAY_OF_MONTH, 1); 76 beginOfCM.set(Calendar.HOUR_OF_DAY, 0); 77 beginOfCM.set(Calendar.MINUTE, 0); 78 beginOfCM.set(Calendar.SECOND, 0); 79 starttime=sdf.format(beginOfCM.getTime()); 80 Calendar endOfCM = Calendar.getInstance(); 81 endOfCM.set(Calendar.DAY_OF_MONTH, endOfCM.getActualMaximum(Calendar.DAY_OF_MONTH)); 82 endOfCM.set(Calendar.HOUR_OF_DAY, 23); 83 endOfCM.set(Calendar.MINUTE, 59); 84 endOfCM.set(Calendar.SECOND, 59); 85 endtime=sdf.format(endOfCM.getTime()); 86 feedbacks=feedbackService.listFeedbacks(start,end,starttime,endtime); 87 } 88 ControllerResult controllerResult=new ControllerResult(); 89 controllerResult.getOp().put("code","Y"); 90 controllerResult.getOp().put("info","执行成功"); 91 if(feedbacks==null){ 92 controllerResult.getOp().put("code","N"); 93 controllerResult.getOp().put("info","执行失败"); 94 } 95 controllerResult.setData(feedbacks); 96 return controllerResult; 97 }
Service
1 public List<Map<String, Object>> listFeedbacks(int start, int end, String startTime, String endTime) { 2 3 List<Map<String,Object>> listImge=feedbackMapper.listImges(); 4 List<Map<String,Object>> listFeedbacks=feedbackMapper.listFeedbacks(start,end,startTime,endTime); 5 for(int i=0;i<listFeedbacks.size();i++){ 6 List<Map<String,String>> imges=new ArrayList(); 7 8 for(int j=0;j<listImge.size();j++){ 9 Map imgeandthumbnail=new HashMap(); 10 if(listFeedbacks.get(i).get("f_id")== listImge.get(j).get("f_feedback_id")){ 11 imgeandthumbnail.put("imge",listImge.get(j).get("f_img_url")); 12 imgeandthumbnail.put("thumbnail",listImge.get(j).get("f_thumbnail_url")); 13 imges.add(imgeandthumbnail); 14 } 15 } 16 listFeedbacks.get(i).put("imges",imges); 17 } 18 19 return listFeedbacks; 20 }
Mapper
1 @Select("select a.f_id" + 2 " ,a.f_platform" + 3 " ,c.f_sub_name" + 4 " ,a.f_content" + 5 " ,a.f_createuser" + 6 " ,a.f_createtime" + 7 // "user_id " + 8 " from t_feedback a " + 9 " join t_dict_sub c on c.f_sub_no = a.f_dic_sub" + 10 " where a.f_createtime>#{startTime} and a.f_createtime<#{endTime}"+ 11 " order by a.f_id desc" + 12 " limit #{start},#{end}") 13 //分页 14 //条件限制 15 List<Map<String,Object>> listFeedbacks(@Param("start")int start 16 , @Param("end")int end 17 , @Param("startTime")String startTime 18 , @Param("endTime")String endTime); 19 20 21 22 @Select("select f_feedback_id" + 23 " ,f_img_url" + 24 " ,f_thumbnail_url " + 25 " from t_feedback_image") 26 List<Map<String,Object>> listImges();
以上是关于java实现本月本周本日的主要内容,如果未能解决你的问题,请参考以下文章
MS SQL Server查询 本日本周本月本季度本年起始时间