'how to get age with localdate by instantiate [closed]
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 1:[1]
This works:
public static void main(String[] args) {
LocalDate birthDate = LocalDate.of(1970, 5, 17);
LocalDate currentDate = LocalDate.now();
int age = Period.between(birthDate, currentDate).getYears();
System.out.println("Age is: " + age);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ruggierin |
