Issue
How can I got the age with localdate using the set of the instantiate, i ve try using localdate with set age and it doesnt work. ive tried to put the age as an LocalDate instead of an Int, but it doesnt work too, im a new programmer, i hope you guys could help me solve this.Its a compile error
package main;
import Usuarios.User;
import java.time.LocalDate;
/**
*
* @author Gustavo
*/
public class Main {
public static void main(String[] args) {
User person1 = new User();
person1.setName("Mike");
person1.setbirthdateUser(LocalDate.of(1970, 5, 17));
person1.setAge(LocalDate.now().getYear() - birthdateUser.getYear());
/*person1.age = LocalDate.now().getYear() - person1.birthdateUser.getYear();*/
System.out.println(person1.getName() + " has " + person1.getAge() + " years old");
Solution
The compiling error in this line:
person1.setAge(LocalDate.now().getYear() - birthdateUser.getYear());
is due to the fact that birthdateUser.getYear()
didn't use the Person
object person1
to invoke the method. That should've been
person1.setAge(LocalDate.now().getYear() - person1.getBirthdateUser().getYear());
That said, in my opinion, setting age
on the object is not necessary. It is better to pass the birth date and have the object calculate the age based on this piece of data. So, my solution for this (using Java record) is as follows:
public class Main {
public static void main(String[] args) {
String name = "Mike";
LocalDate birthdate = LocalDate.of(1970, 5, 17);
User person1 = new User(name, birthdate);
System.out.println(person1.name() + ", born in " + person1.birthdate + ", is " + person1.age() + " years old");
}
private static record User (String name, LocalDate birthdate) {
public long age() {
return ChronoUnit.YEARS.between(birthdate, LocalDate.now());
}
}
}
This outputs:
Mike, born in 1970-05-17, is 51 years old
Notice how, even though I am using a record, I can still create a custom method that returns the age based on the birth date passed to the object being invoked. This is actually pretty cool because, even though the age changes, because I am not storing it, it doesn't violate the constraint of the object being "immutable" because age is not a field in the record. It is a calculated value. This makes this object inherently thread safe (not totally).
UPDATE: A slightly better version with more utility methods.
public class Main {
public static void main(String[] args) {
String name = "Mike";
LocalDate birthdate = LocalDate.of(1970, 5, 17);
User person1 = new User(name, birthdate);
System.out.println(person1.toString());
System.out.println(person1.name() + " was born on a " + person1.getDayBornOn());
}
private static record User (String name, LocalDate birthdate) {
public long age() {
return ChronoUnit.YEARS.between(birthdate, LocalDate.now());
}
public int getBirthYear() {
return birthdate.getYear();
}
public String getBirthdateMonth() {
return capitalizeFirstLetter(birthdate.getMonth().toString());
}
public String getBirthday() {
return getBirthdateMonth() + " " + birthdate.getDayOfMonth();
}
public String getDateOfBirth() {
return getBirthday() + ", " + getBirthYear();
}
public String getDayBornOn() {
return capitalizeFirstLetter(birthdate.getDayOfWeek().name());
}
private String capitalizeFirstLetter(String string) {
string = string.toLowerCase();
String upperLetter = String.valueOf(string.charAt(0)).toUpperCase();
return upperLetter + string.substring(1);
}
@Override
public String toString() {
return this.name() + ", born in " + this.getDateOfBirth() + ", is " + this.age() + " years old";
}
}
}
Now this outputs:
Mike, born in May 17, 1970, is 51 years old
Mike was born on a Sunday
Answered By - hfontanez
Answer Checked By - Willingham (JavaFixing Volunteer)