Java 如何在考虑闰年的同时从出生日期和当前日期计算年龄?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30475537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:41:41  来源:igfitidea点击:

How do I calculate age from date of birth and current date while taking leap year into account?

java

提问by Brian T Hannan

I have date of birth and can get the current date.

我有出生日期,可以获取当前日期。

In java, how can I calculate someone's age while taking leap year into account?

在java中,如何在考虑闰年的同时计算某人的年龄?

Edit: Can I use unix timestamps and compare the difference?

编辑:我可以使用 unix 时间戳并比较差异吗?

回答by Krzysztof Cichocki

What about:

关于什么:

    Date birthDate = new Date(85, 03, 24);

    GregorianCalendar birth = new GregorianCalendar();
    birth.setTime(birthDate);
    int month = birth.get(GregorianCalendar.MONTH);
    int day = birth.get(GregorianCalendar.DAY_OF_MONTH);

    GregorianCalendar now = new GregorianCalendar();

    int age = now.get(GregorianCalendar.YEAR) - birth.get(GregorianCalendar.YEAR);

    int birthMonth = birth.get(GregorianCalendar.MONTH);
    int birthDay = birth.get(GregorianCalendar.DAY_OF_MONTH);
    int nowMonth = now.get(GregorianCalendar.MONTH);
    int nowDay = now.get(GregorianCalendar.DAY_OF_MONTH);

    if (nowMonth>birthMonth) {
        age = age+1;
    } else {
        if (nowMonth == birthMonth) {
            if (nowDay >= birthDay) {
                age= age+1;
            }
        }
    }   
    System.out.println("Now it is my " + age+ " year of life");

回答by Uma Kanth

As @MadProgrammer has suggested, you could use JodaTime.

正如@MadProgrammer 所建议的,您可以使用JodaTime.

Here's the sample code.

这是示例代码。

LocalDate birthdate = new LocalDate (1970, 1, 20);
LocalDate now = new LocalDate();
Years age = Years.yearsBetween(birthdate, now);

回答by Yogesh Funde

As you may know that java 8 date and time API changes are inspired from Jodatime library itself, so out next solution using java 8 looks almost similar to above code sample:

您可能知道 java 8 日期和时间 API 更改的灵感来自 Jodatime 库本身,因此使用 java 8 的下一个解决方案看起来几乎类似于上面的代码示例:

LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);

Period p = Period.between(birthday, today);

//Now access the values as below
System.out.println(p.getDays());
System.out.println(p.getMonths());
System.out.println(p.getYears());

回答by Soumitri Pattnaik

    LocalDate birthdate = new LocalDate (1990, 12, 2);
    LocalDate now = new LocalDate();
    Years age = Years.yearsBetween(birthdate, now);

回答by NDY

In Java 8:

Java 8

LocalDate startDate = LocalDate.of(1987, Month.AUGUST, 10);
LocalDate endDate = LocalDate.of(2015, Month.MAY, 27);

long numberOfYears = ChronoUnit.YEARS.between(startDate, endDate);

Great examples for using dates with Java 8: Java 8 Date Examples

使用日期的绝佳示例Java 8Java 8 日期示例