You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package test;
|
|
|
|
|
|
|
|
import java.time.LocalDate;
|
|
|
|
import java.time.Period;
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Author weaver_cl
|
|
|
|
* @Description:
|
|
|
|
* @Date 2022/10/9
|
|
|
|
* @Version V1.0
|
|
|
|
**/
|
|
|
|
public class MainTest {
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|