-1

I have three classes. My driver class, a Person class, and a Chore class.

I have chores and family members listed in two separate csv files.

The family members file just contains names and dob's (01/01/1901). So the first thing I want to do is calculate each family member's age based on the year in their dob and get the current year then get the difference of the two. How do I select for just the year in each person's dob and how do I get the current year?

   public int currentAge(LocalDate dob, LocalDate currentDate){
            if ((dob != null) && (currentDate != null)) {
                return Period.between(dob, currentDate).getYears();
            } else {
                return 0;
            }
        }
        public int getCurrentAge(){
            return currentAge;
        }

Yep
  • 47
  • 8
  • [I downvoted because a wall of code isn't helpful](http://idownvotedbecau.se/toomuchcode/). Please train [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Ole V.V. Mar 26 '20 at 02:43
  • I recommend you don’t use `Date` at all. That class is poorly designed and long outdated. Stay with `LocalDate` and `Period` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 26 '20 at 02:46
  • As an aside: [Anti-pattern: parallel collections](https://codeblog.jonskeet.uk/2014/06/03/anti-pattern-parallel-collections/). – Ole V.V. Mar 26 '20 at 02:49

3 Answers3

4

To calculate the age of a person from their birthdate, using Java 8+ Time API, use this method:

public static int getAge(LocalDate dob) {
    LocalDate today = LocalDate.now();
    int age = today.getYear() - dob.getYear();
    if (MonthDay.from(today).isBefore(MonthDay.from(dob)))
        age--; // before birthday in current year
    return age;
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
1

Java 8 compatible solution :

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;

public class Demo
{
    public static void main(String[] args)
    {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");
        String date = "16/08/2016";
        LocalDate dob = LocalDate.parse(date, formatter);
        System.out.println(daysBetween(dob));
    }

    private static long daysBetween(LocalDate dob)
    {
        return Period
            .between(dob,
                LocalDate.now())
            .getYears();

    }
}

We can add a data member in the People for holding the chore information.

class People{
    private final String name;
    private final LocalDate dob;
    private final String gender;
    private List<Chores> choresList;
    }
0

You can use the following method or something similar to get the age:

private long daysBetween(Date one, Date two) {
    long difference =  (one.getTime()-two.getTime())/86400000;
    return Math.abs(difference);
}

Read more here or here.

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108