allen123596 2016-12-06 02:37 采纳率: 0%
浏览 1036
已结题

求个大神帮我解决一下,C币好说,很急

图片说明

  • 写回答

3条回答

  • luoganlin 2016-12-06 03:14
    关注

    // String birth = "2000-08-05";
    String birth = "2000-09-15";
    String today = "2011-09-12";
    SimpleDateFormat ageFormat = new SimpleDateFormat("yyyy-MM-dd");

     try {
        Date birthDayD = ageFormat.parse(birth);
        Date todayD = ageFormat.parse(today);
    
        int birthYear = birthDayD.getYear();
        int birthMonth = birthDayD.getMonth();
        int birthDay = birthDayD.getDay();
    
        int todayYear = todayD.getYear();
        int todayMonth = todayD.getMonth();
        int todayDay = todayD.getDay();
    
        int age = todayYear - birthYear;
    
        if(todayMonth - birthMonth < 0){
        age = age - 1;
        } else if(todayMonth - birthMonth == 0){
        if(todayDay - birthDay < 0){
            age = age - 1;
        }
        }
        System.out.println("age=" + age);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    评论

报告相同问题?