如何通过实例化获取 localdate 的年龄
how to get age with localdate by instantiate
我如何使用实例化集获取 localdate 的年龄,我尝试使用 localdate 和设置的年龄,但它不起作用。我试过将年龄作为 LocalDate 而不是 Int,但它也不起作用,我是一个新程序员,我希望你们能帮我解决 this.Its 编译错误
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");
这一行编译错误:
person1.setAge(LocalDate.now().getYear() - birthdateUser.getYear());
是因为birthdateUser.getYear()
没有使用Person
对象person1
来调用方法。应该是
person1.setAge(LocalDate.now().getYear() - person1.getBirthdateUser().getYear());
也就是说,在我看来,没有必要在对象上设置 age
。最好传递出生日期,让对象根据这条数据计算年龄。因此,我对此(使用 Java 记录)的解决方案如下:
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());
}
}
}
这输出:
Mike, born in 1970-05-17, is 51 years old
请注意,即使我使用的是记录,我仍然可以创建一个自定义方法,该方法 returns 根据传递给被调用对象的出生日期计算年龄。这实际上非常酷,因为即使年龄发生变化,因为我没有存储它,它也不会违反对象“不可变”的约束,因为年龄不是记录中的字段。它是一个计算值。这使得这个对象本质上是线程安全的(不完全)。
更新:一个稍微好一点的版本,有更多实用方法。
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";
}
}
}
现在输出:
Mike, born in May 17, 1970, is 51 years old
Mike was born on a Sunday
这个有效:
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);
}
我如何使用实例化集获取 localdate 的年龄,我尝试使用 localdate 和设置的年龄,但它不起作用。我试过将年龄作为 LocalDate 而不是 Int,但它也不起作用,我是一个新程序员,我希望你们能帮我解决 this.Its 编译错误
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");
这一行编译错误:
person1.setAge(LocalDate.now().getYear() - birthdateUser.getYear());
是因为birthdateUser.getYear()
没有使用Person
对象person1
来调用方法。应该是
person1.setAge(LocalDate.now().getYear() - person1.getBirthdateUser().getYear());
也就是说,在我看来,没有必要在对象上设置 age
。最好传递出生日期,让对象根据这条数据计算年龄。因此,我对此(使用 Java 记录)的解决方案如下:
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());
}
}
}
这输出:
Mike, born in 1970-05-17, is 51 years old
请注意,即使我使用的是记录,我仍然可以创建一个自定义方法,该方法 returns 根据传递给被调用对象的出生日期计算年龄。这实际上非常酷,因为即使年龄发生变化,因为我没有存储它,它也不会违反对象“不可变”的约束,因为年龄不是记录中的字段。它是一个计算值。这使得这个对象本质上是线程安全的(不完全)。
更新:一个稍微好一点的版本,有更多实用方法。
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";
}
}
}
现在输出:
Mike, born in May 17, 1970, is 51 years old
Mike was born on a Sunday
这个有效:
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);
}