用JAVA写一个程序:输入一个人的出生日期和当前的日期(202x,x,xx),输出其实足年龄。
时间: 2024-05-26 17:10:08 浏览: 131
没问题,请输入以下代码:
import java.time.LocalDate;
import java.time.Period;
import java.util.Scanner;
public class AgeCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入出生日期(格式:yyyy-MM-dd):");
String birthDateString = scanner.nextLine();
LocalDate birthDate = LocalDate.parse(birthDateString);
LocalDate currentDate = LocalDate.now();
Period period = Period.between(birthDate, currentDate);
int age = period.getYears();
System.out.println("该人的年龄为:" + age + "岁");
}
}
阅读全文