1

Im trying to calculate a users age based on their DOB input and compare it with todays date to work out their age in years. If they are under 12 they will be denied.

int YearDiff;
public void ageCalc()
{
    ZoneId nowBrit = ZoneId.of("Europe/London");
    LocalDate timeBrit = LocalDate.now(nowBrit);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
    int YearNow = Integer.parseInt(sdf.format(timeBrit));
    int YearSel = Integer.parseInt(sdf.format(jDateChooserDOB.getDate()));
    YearDiff = YearNow - YearSel;
}

The YearDiff variable will then be tested to see if its less than 12 in an if statement however it always returns 0.

Also if there is a better way to do age verification please do share.

Thanks in advance

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Dzl
  • 49
  • 8
  • Where do you return yearDiff? – Rcordoval Dec 05 '18 at 05:45
  • The only other time YearDiff is mention again is a public boolean if else statemnt to verify data. else if(YearDiff < 12) – Dzl Dec 05 '18 at 06:22
  • 1
    Can you print YearNow and YearSel after you set them? – Rcordoval Dec 05 '18 at 06:38
  • I cannot reproduce. Instead I get `java.lang.IllegalArgumentException: Cannot format given Object as a Date` from the line `int YearNow = Integer.parseInt(sdf.format(timeBrit));`. – Ole V.V. Dec 05 '18 at 11:30
  • Since you can use java.time, the modern Java date and time API, (`ZoneId`, `LocalDate`), use it exclusively and don’t mix the old and outdated classes like `SimpleDateFormat` in. Since `jDateChooserDOB.getDate()` returns an old-fashioned `Date`, convert it using its `toInstant` method and perform further conversion from there. Also formatting the year and parsing it back is the detour. Just use `timeBrit.getYear()`. – Ole V.V. Dec 05 '18 at 11:33
  • Calculating someone’e age has been covered many times on Stack Overflow. Please use your search engine. Just one example: [How do I calculate someone's age in Java?](https://stackoverflow.com/questions/1116123/how-do-i-calculate-someones-age-in-java) – Ole V.V. Dec 05 '18 at 11:36
  • Thanks for your detailed reply! I had a look over it again not i've slept and I realised the ageCalc method wasn't called properly and it was just pulling int YearDiff;, hence why I wasn't get that error. And you're correct in saying this isn't a good way for reasons youve stated, ill look into your example and try to creat something with that. Thanks for your help! – Dzl Dec 05 '18 at 16:40

1 Answers1

1

The old-fashioned SimpleDateFormat class cannot format any of the modern objects of the java.time classes like LocalDate. Therefore the following line throws an exception:

    int YearNow = Integer.parseInt(sdf.format(timeBrit));

The exception is java.lang.IllegalArgumentException: Cannot format given Object as a Date. This causes the remainder of your method not to be executed. And therefore you are never assigning any value to YearDiff. It being a field it has a default value of 0, which is therefore the result you see.

Apparently you didn’t see the exception? If this is so, you’ve got a more serious problem than an incorrect result, either in your code (an empty catch block, known as “swallowing exceptions”?) or you project setup. Please fix first thing.

Where I come from people get one year older on their birthday, not at New Year (when the year changes). I know it is not so everywhere in the world. In any case please search the Internet for the correct way of calculating an age according to your culture. Only skip the pages using the old and outdated classes like Date, Calendar, GregorianCalendar, DateFormat and SimpleDateFormat. java.time is so much nicer to work with and has better support for age calculation.

PS Since jDateChooserDOB.getDate() returns an old-fashioned Date, convert it to Instant and perform further conversions from there, for example:

    int yearSel = jDateChooserDOB.getDate().toInstant().atZone(nowBrit).getYear();
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Took your advice and looked elsewhere for solutions. JodaTime was able to get it working easily with stronger code. Thanks again – Dzl Dec 05 '18 at 17:42
  • 1
    Glad I could point you in the right direction. Joda-Time is a fine library, but is now in maintenance mode. java.time is its successor, developed by the same people, so for a more future-proof solution, you may want to check it out once more. – Ole V.V. Dec 05 '18 at 17:45