jdk8计算一个人的年龄

package org.example;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;

public class YearCalcaulateTest {
    public static void main(String[] args) {
        String birthday = "1995-11、29";
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        try {
            LocalDate startDate = LocalDate.parse(birthday, dateTimeFormatter);
            LocalDate now = LocalDate.now();
            Period period = Period.between(startDate, now);
            System.out.println(period.getYears());
        } catch (Exception e) {
            System.out.println("请检查出生日期格式");
        }

    }
}

你可能感兴趣的:(java)