-1

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

user3331920
  • 41
  • 1
  • 3
  • 10

2 Answers2

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