如何准确给出给定出生日期的Java年龄 [英] How can I calculate age in Java accurately given Date of birth

查看:68
本文介绍了如何准确给出给定出生日期的Java年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用占月数的Java计算年龄,因此仅减去年数将不起作用。我也想告诉用户今天是否是他们的生日。这是我到目前为止的代码,但恐怕会有点问题。即使比较的两个日期相等,它也不会告诉您今天是否是生日。我最初尝试计算的方法是使用毫秒。之所以看到两种获取当前日期的方法,是因为我正在尝试使日期生效,但是想向所有人展示我的工作,以便他们可以向我指出正确的方向。

I am trying to calculate age in java that accounts for months, so just subtracting years will not work. I also want to tell the user if today is their birthday. Here is the code I have so far, but I am afraid it is a bit off. It also will not tell if today is the birthday even though the two dates it's comparing are equal. The way I tried to originally calculate was using milliseconds. The reason you see 2 ways of getting the current date is because I was trying something to get it working, but wanted to show everyone my work so that they could point me in the right direction.

澄清说明
我的意思是2015-1993年可以是22岁,也可以是21岁,具体取决于他们的生日是否已经过今年。我想确定自己正确的年龄。

EDIT FOR CLARIFICATION what I mean is 2015-1993 can either be 22 years old or 21 depending if their birthday has already passed this year. I want to be sure that I get the correct age with this in mind.

public class ShowAgeActivity extends AppCompatActivity {

private TextView usersAge;

private static long daysBetween(Date one, Date two)
{
    long difference = (one.getTime()-two.getTime())/86400000; return Math.abs(difference);
}

private Date getCurrentForBirthday()
{
    Date birthday = (Date) this.getIntent().getExtras().get("TheBirthDay");
    int birthdayYear = birthday.getYear() + 1900;
    Calendar cal = Calendar.getInstance();
    cal.set(birthdayYear, Calendar.MONTH, Calendar.DAY_OF_MONTH);
    Date current = cal.getTime();
    return current;
}


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_age);


    Date birthday = (Date) this.getIntent().getExtras().get("TheBirthDay");
      Date currentDay = Calendar.getInstance().getTime();

      long age = daysBetween(birthday,currentDay)/365;

      usersAge =(TextView)findViewById(R.id.ageTextView);

    if (birthday.compareTo(getCurrentForBirthday()) == 0 )
    {
        usersAge.setText("It is your birthday, and your Age is " + String.valueOf(age));
    }

    usersAge.setText("Your Age is " + String.valueOf(age));


    }

}

推荐答案

下面是一个如何计算一个人的年龄(如果今天是他们的生日)以及到今天为止还剩下多少天的示例。不是使用Java 8中包含的新java.time包类的生日。

Below is an example of how to calculate a person's age, if today is their birthday, as well as how many days are left until their birthday if today is not their birthday using the new java.time package classes that were included as a part of Java 8.

  LocalDate today             = LocalDate.now();
  LocalDate birthday          = LocalDate.of(1982, 9, 26);
  LocalDate thisYearsBirthday = birthday.with(Year.now());

  long age = ChronoUnit.YEARS.between(birthday, today);

  if (thisYearsBirthday.equals(today))
  {
     System.out.println("It is your birthday, and your Age is " + age);
  }
  else
  {
     long daysUntilBirthday = ChronoUnit.DAYS.between(today, thisYearsBirthday);
     System.out.println("Your age is " + age + ". " + daysUntilBirthday + " more days until your birthday!");
  }

这篇关于如何准确给出给定出生日期的Java年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆