0

In one of my constructors i have :

public Author(String firstName, String lastName, String middleName, int yearBorn, 
    int monthBorn, int dayBorn, int yearDied, int monthDied, int dayDied,
    String pseudonymFirstName, String pseudonymLastName, String pseudonymMiddleName ){
        name = new Name(firstName,lastName,middleName);
        born = new Date(yearBorn, monthBorn, dayBorn);
        died = new Date(yearDied, monthDied, dayDied);  

 if((pseudonymFirstName == null) && (pseudonymLastName == null) && (pseudonymMiddleName == null)){
            pseudonym = null;
        }else{
            pseudonym = new Name(pseudonymFirstName,pseudonymLastName,pseudonymMiddleName);
        }

    } 

this constructor calls methods in other classes to store values for the Author class.

now i want to make a method that subtracts the value of yearBorn from the current year giving me the age of the Author

so far my methond looks like:

public int getAgeYearsOfAuthor(){

        return CURRENT_YEAR - (WHAT DO I PUT HERE?????);

}

how do i extract the yearBorn value (which is an int) form: born = new Date(yearBorn, monthBorn, dayBorn); in the constructor??

Rohan
  • 3,068
  • 1
  • 20
  • 26

3 Answers3

0

You can try this method

public int getAgeYearsOfAuthor(){
  return CURRENT_YEAR -(born.getYear)
}

but be careful if you use Date from JAVA it's constructor when you set year it will add 1900 to you variable

Adrian Totolici
  • 223
  • 2
  • 20
0

Try this:

LocalDate now = new LocalDate();
Years age = Years.yearsBetween(born, now);

See the original answer here

And make sure that your born Date is a correct LocalDate()

Community
  • 1
  • 1
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
0

In java8 you can do:

final LocalDate now = LocalDate.now();
final LocalDate birt = LocalDate.of(1980, Month.DECEMBER, 10);
final Period p = Period.between(birt, now);
System.out.println("You are " + p.getYears() + " years old.");
jjlema
  • 850
  • 5
  • 8