Java根据时间计算是否为上午、中午、下午以及根据日期获得星期几

2018-04-14 21:04:37
1017次阅读
0个评论


/** 
     * 根据小时判断是否为上午、中午、下午 
     * @param hour 
     * @return 
     * @author zhangsq 
     */  
    public static String getDuringDay(int hour){  
        if (hour >= 7 && hour < 11) {  
            return BasicConstants.AM;  
        }if (hour >= 11 && hour <= 13) {  
            return BasicConstants.NOON;  
        }if (hour >= 14 && hour <= 18) {  
            return BasicConstants.PM;  
        }  
        return null;  
    }

2、根据根据日期获得星期几的方法


/** 
     * 根据日期获得星期的方法 
     * @param date 
     * @return 
     * @author zhangsq 
     */  
    public static String getWeekOfDate(Date date) {   
          String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };   
          //String[] weekDaysCode = { "0", "1", "2", "3", "4", "5", "6" };   
          Calendar calendar = Calendar.getInstance();   
          calendar.setTime(date);   
          int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;   
          return weekDaysName[intWeek];   
        }


@RequestMapping(value="/toHmplan.do")  
    public @ResponseBody List<Map<String, Object>> getIndexHmplanInfo(HttpServletRequest request,  
            HttpServletResponse response,ModelMap model,Integer pageMin,Integer pageMax){  
        SimpleDateFormat dfTimes = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        List<HmPlan> hmPlansList = indexHmPlanService.findHmplanInfo(pageMin,pageMax);  
        List<Map<String, Object>> hmPlanListMap = new ArrayList<Map<String, Object>>();  
        for(HmPlan hmPlanBean : hmPlansList){  
            String planTimeStr = dfTimes.format(hmPlanBean.getPlantTime());  
            hmPlanBean.setDates(planTimeStr.substring(0, 10));//年月日  
            hmPlanBean.setTimes(planTimeStr.substring(11));//时分秒  
            if(planTimeStr.length()>10){  
                //上午、中午、下午  
                hmPlanBean.setDuring(WeekDayUtils.getDuringDay(Integer.parseInt(hmPlanBean.getTimes().substring(0,2))));  
            }else {  
                hmPlanBean.setDuring(BasicConstants.DURING);  
            }  
            //根据日期获取星期  
            hmPlanBean.setWeekDay(WeekDayUtils.getWeekOfDate(hmPlanBean.getPlantTime()));  
            Map<String, Object> hmPlanMap = new HashMap<String, Object>();  
            hmPlanMap.put("hmPlanBean", hmPlanBean);  
              
            hmPlanListMap.add(hmPlanMap);  
        }  
        return hmPlanListMap;  
    }


收藏00

登录 后评论。没有帐号? 注册 一个。