I have a date of birth in 06-03-2014(dd-mm-yyyy) format. now I want to check if the age of particular employee is equal to or above 18 years comparing with todays date. How can i do this in java.Please help
Asked
Active
Viewed 8,934 times
-1
-
http://stackoverflow.com/questions/1116123/how-do-i-calculate-someones-age-in-java – Jay Mar 27 '14 at 12:01
-
4Stackoverflow is not a coding service to fulfill your requirement.Please post what you have tried so far! – Keerthivasan Mar 27 '14 at 12:01
2 Answers
2
You may try it this way:
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));
}

Harmlezz
- 7,972
- 27
- 35
0
You can try this
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..");
}

Sathesh
- 378
- 1
- 4
- 13
-
why diffDays which represented for days from current date get compared with year? – Toan Le Oct 25 '18 at 16:02