根據日期判斷是否滿18歲成年


        //使用Calendar類代表當前時間
        Calendar c = Calendar.getInstance();
        //examinee.getBaseInfo().getBirthday()獲取前端頁面輸入的日期
        Date ss =examinee.getBaseInfo().getBirthday();
        //date轉換為calendar類
        Calendar cal = Calendar.getInstance();
        cal.setTime(ss);
        //比較年份 當前年份-輸入年份大於18--已成年
        Integer year = c.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
        //比較月份,相同年份用月份做判斷,當前月分-輸入月份大於0--為未成年,負數為已成年
        Integer month = c.get(Calendar.MONTH) - cal.get(Calendar.MONTH);
        //比較日,相同月份,用日做判斷,當前日-輸入日大於0--為未成年,負數為已成年
        Integer date = c.get(Calendar.DAY_OF_MONTH) - cal.get(Calendar.DAY_OF_MONTH);
        //year年份大於18為已成年,month為正數且大於0且year大於18為已成年,date為正數且大於0且month為正數大於0且year大於18為已成年
          if (year < 18 || month > 0 || date>0 ) {
              throw new ServiceLayerException("不滿18歲!");
          }                

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM