2014-03-27 288 views
-1

我有一個出生日期爲06-03-2014(dd-mm-yyyy)格式。現在我想檢查一下特定員工的年齡是否與今天的日期相等或超過18年。 我怎樣才能做到這一點java.Please幫助在java中檢查年齡大於或等於18歲的日期驗證

+0

http://stackoverflow.com/questions/1116123/how-do-i-calculate-someones-age-in-java – Jay

+3

Stackoverflow不是一個能夠滿足你的需求的編碼服務。請發佈你到目前爲止嘗試過的東西! – Keerthivasan

回答

0

你可以試試這樣說:

public static void main(String[] args) throws Exception { 
    String dateString = "06-03-2014"; 
    Date date = new SimpleDateFormat("dd-MM-yyyy").parse(dateString); 
    Calendar calendar = GregorianCalendar.getInstance(); 
    calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 18); 
    System.out.printf("Date %s is older than 18? %s", dateString, calendar.getTime().after(date)); 
} 
0

你可以試試這個

DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); 
Date date = new Date(); 
System.out.println("Current Date : "+dateFormat.format(date)); 
String dateInString = "24-03-2014"; 
Date date1 = dateFormat.parse(dateInString); 
System.out.println(dateFormat.format(date1)); 
long diff = Math.abs(date.getTime() - date1.getTime()); 
long diffDays = diff/(24 * 60 * 60 * 1000); 
System.out.println("Difference"+diffDays); 
if(diffDays > 18){ 
    System.out.println("The employee above 18.."); 
}else{ 
    System.out.println("The employee below 18.."); 
} 
相關問題