//使用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歲!"); }