2023-06-14 13:42:14 +08:00
|
|
|
package test;
|
|
|
|
|
|
2024-11-05 18:31:33 +08:00
|
|
|
import java.time.LocalDate;
|
|
|
|
|
import java.time.Period;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
|
2023-06-14 13:42:14 +08:00
|
|
|
/**
|
|
|
|
|
* @Author weaver_cl
|
|
|
|
|
* @Description:
|
|
|
|
|
* @Date 2022/10/9
|
|
|
|
|
* @Version V1.0
|
|
|
|
|
**/
|
|
|
|
|
public class MainTest {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
2024-11-05 18:31:33 +08:00
|
|
|
String birthDateString = "1997-11-09";
|
|
|
|
|
// 定义日期格式
|
|
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
|
|
// 解析生日字符串为 LocalDate 对象
|
|
|
|
|
LocalDate birthDate = LocalDate.parse(birthDateString, formatter);
|
|
|
|
|
// 获取当前日期
|
|
|
|
|
LocalDate currentDate = LocalDate.now();
|
|
|
|
|
// 计算年龄
|
|
|
|
|
System.out.println(Period.between(birthDate, currentDate).getYears());
|
2023-06-14 13:42:14 +08:00
|
|
|
}
|
|
|
|
|
}
|