的Andr​​oid如何改变天总数量为年,月,天到底是什么?到底是什么、量为、总数、Andr

2023-09-07 23:55:30 作者:随风

我这样做是根据生日日期给定的输入得到一个人的年龄相关的应用程序。对此,我自该日起,从下面code当前日期越来越天总数。

i am doing an app that is related to get the age of a person according to the given input of birthday date. for that i am getting the total number of days from that date to the current date from the below code.

      String strThatDay = "1991/05/10";
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
      Date d = null;
      try {

      try {
      d = formatter.parse(strThatDay);
      Log.i(TAG, "" +d);
      } catch (java.text.ParseException e) {

      e.printStackTrace();
      }
      } catch (ParseException e) {

      e.printStackTrace();
      } 
      Calendar thatDay = Calendar.getInstance();
      thatDay.setTime(d); //rest is the same....

      Calendar today = Calendar.getInstance();
      long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); 
      long days = diff / (24 * 60 * 60 * 1000);

从这个code我得到总天数。所以我的要求是转换日的总数到年,月,日正好..请帮助....

from this code i am getting total number of days. so my requirement is convert the total number of days in to years ,months and days exactly.. please help....

推荐答案

您应该使用Duration类:

You should use the Duration class :

Duration duration = new Duration();
duration.add( today );
duration.substract( birthDate);
int years = duration.getYears();
int months = duration.getMonths();
int days = duration.getDays();

其他的一些替代方案包括使用一个专门的时间管理库:乔达时间。见Calculate年龄在年,月,日,时,分,秒