Java根据出生日期计算还有几天到生日

2018-04-14 21:02:48
895次阅读
0个评论
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");  
        String clidate = "1978-03-30";  
        Calendar cToday = Calendar.getInstance(); // 存今天  
        Calendar cBirth = Calendar.getInstance(); // 存生日  
        cBirth.setTime(myFormatter.parse(clidate)); // 设置生日  
        cBirth.set(Calendar.YEAR, cToday.get(Calendar.YEAR)); // 修改为本年  
        int days;   
        if (cBirth.get(Calendar.DAY_OF_YEAR) < cToday.get(Calendar.DAY_OF_YEAR)) {  
            // 生日已经过了,要算明年的了  
            days = cToday.getActualMaximum(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR);  
            days += cBirth.get(Calendar.DAY_OF_YEAR);  
        } else {  
            // 生日还没过  
            days = cBirth.get(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR);  
        }  
        // 输出结果  
        if (days == 0) {  
            System.out.println("今天生日");  
        } else {  
            System.out.println("距离生日还有:" + days + "天");  
        }
@RequestMapping(value="/toPerBirthday.do")  
    public @ResponseBody List<Map<String, Object>> getIndexPerBasicInfo(HttpServletRequest request,  
            HttpServletResponse response,ModelMap model) throws Exception{  
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");  
        //查询人员生日信息  
        List<PerBasicInfo> pBasicInfos = personBasicInfoService.findPerBirthdayDay();  
        List<Map<String, Object>> brithdayList = new ArrayList<Map<String, Object>>();  
          
        for(PerBasicInfo pb : pBasicInfos){  
            Map<String, Object> perBaiscMap = new HashMap<String, Object>();  
            String birthdayStr = myFormatter.format(pb.getBirthday());  
              
            Calendar cToday = Calendar.getInstance(); // 存今天  
            Calendar cBirth = Calendar.getInstance(); // 存生日  
            cBirth.setTime(myFormatter.parse(birthdayStr)); // 设置生日  
            cBirth.set(Calendar.YEAR, cToday.get(Calendar.YEAR)); // 修改为本年  
            int days;   
            if (cBirth.get(Calendar.DAY_OF_YEAR) < cToday.get(Calendar.DAY_OF_YEAR)) {  
                // 生日已经过了,要算明年的了  
                days = cToday.getActualMaximum(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR);  
                days += cBirth.get(Calendar.DAY_OF_YEAR);  
            } else {  
                // 生日还没过  
                days = cBirth.get(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR);  
            }  
              
            perBaiscMap.put("name", pb.getName());  
            perBaiscMap.put("birthday", birthdayStr);  
            perBaiscMap.put("time", birthdayStr.substring(5, 7).concat(".").concat(birthdayStr.substring(8, 10)));  
            perBaiscMap.put("type", "0");  
            if (days == BasicConstants.DAY_ZERO) {  
                perBaiscMap.put("type", "1");  
                perBaiscMap.put("msg",BasicConstants.TODAY.concat(pb.getName().replaceAll(",",  "、")).concat(BasicConstants.TEACHER_BRITHDAY));  
            }else if (days == BasicConstants.DAY_ONE) {  
                perBaiscMap.put("type", "1");  
                perBaiscMap.put("msg", BasicConstants.TOMORROW.concat(pb.getName().replaceAll(",", "、")).concat(BasicConstants.TEACHER_BRITHDAY));  
            }else if (days == BasicConstants.DAY_TWO) {  
                perBaiscMap.put("type", "1");  
                perBaiscMap.put("msg", BasicConstants.AFTER_TOMORROW.concat(pb.getName().replaceAll(",", "、")).concat(BasicConstants.TEACHER_BRITHDAY));  
            }  
            if(Integer.parseInt((String)perBaiscMap.get("type")) == 1){  
                brithdayList.add(perBaiscMap);  
            }  
        }  
        return brithdayList;  
    }


收藏00

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