# Check if the user is 18 years old (upto days)

This code is returning 'true' for any date which are later than Feb 24, 1998.

//minimumAge is number of years i.e., 18
public boolean isUser18Older (DateTime userDob, int minimumAge){
DateTime minAge = new DateTime();
Days days = Days.daysBetween(userDob, minAge.minusYears(minimumAge));

return days.getDays()>=0 ;
}


I am not sure about my minAge.minusYears code.

• If you are not sure about your minusYears method, you'll have to post that code here along with what improvements you are looking to make. – SirPython Feb 23 '16 at 21:34

There's a potential bug in your method if the user is born just the day after the age requirement. Your method will return true when it should in fact return false. For example the following code prints true:

public static void main(String[] args) {
DateTime test = new DateTime().minusYears(1).plusDays(1);
System.out.println(isUser18Older(test, 1));
}

public static boolean isUser18Older (DateTime userDob, int minimumAge){
DateTime minAge = new DateTime();
Days days = Days.daysBetween(userDob, minAge.minusYears(minimumAge));

return days.getDays()>=0 ;
}


although the user was born the day after the requirement. This is because you're not taking the time into account.

The name of the method is misleading with regard to what it does. It is named isUser18Older, which implies that the method checks whether a user has at least 18 years-old. However, the method takes a minimumAge parameter, which is actually the minimum age requirement. Therefore, I suggest renaming that method hasAgeRequirement.

The code itself could be written more simpler and to-the-point. Instead of getting the number of days between the two dates, just retrieve the number of years between the date of birth and today and compare it with the age requirement:

public static boolean hasAgeRequirement(DateTime userDob, int minimumAge) {
DateTime now = new DateTime();
Years age = Years.yearsBetween(userDob, now);
return age.getYears() >= minimumAge;
}


This is easier to read since it directly codes the intent of the method.

• I want to calculate the age upto the days and not the time or the years. For that reason i have to opt for the path which i showed. yeah, the method name should be better. – Em Ae Feb 23 '16 at 17:48
• @EmAe I think I see your concern. My proposal takes the days into account as well. You can try it with today's date for example. If minimumAge = 1, it will return true for all dates before 23rd Feb 2015 (taking today's date into account). – Tunaki Feb 23 '16 at 17:53
• What difference would be between Years.yearsBetween(...) and using Period(...) class ? – Em Ae Feb 23 '16 at 18:00
• @EmAe A period is different. It is defined by the chrono fields. For example, between today and today + 1 year + 1 month, a period would be "1 year and 1 month". It wouldn't be "13 months". You can refer to this Stack Overflow question: stackoverflow.com/q/2653567/1743880 – Tunaki Feb 23 '16 at 18:49