2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – SirPython
    Feb 23, 2016 at 21:34

1 Answer 1

8
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$
    – Em Ae
    Feb 23, 2016 at 17:48
  • \$\begingroup\$ @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). \$\endgroup\$
    – Tunaki
    Feb 23, 2016 at 17:53
  • \$\begingroup\$ What difference would be between Years.yearsBetween(...) and using Period(...) class ? \$\endgroup\$
    – Em Ae
    Feb 23, 2016 at 18:00
  • \$\begingroup\$ @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 \$\endgroup\$
    – Tunaki
    Feb 23, 2016 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.