我如何計算死亡年齡? [重復] - How can I calculate the age at death? [duplicate] -开发者知识库

我如何計算死亡年齡? [重復] - How can I calculate the age at death? [duplicate] -开发者知识库,第1张

Possible Duplicates:
How can I calculate the age of a person in year, month, days?
How can I calculate the difference between two dates

可能重復:如何計算年,月,日的人的年齡?如何計算兩個日期之間的差異

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
if(petDetails.getDateOfDeath() != null){
    String  formatedDateOfDeath = formatter.format(petDetails.getDateOfDeath());
    String formateDateOfBirth = formatter.format(petDetails.getDateOfBirth());
}

How can i calculate the age of death from the above. I dont want to use any externallibraries

我怎樣才能從上面算出死亡年齡。我不想使用任何外部庫

EDIT: please look at what I've got so far.none of the other threads are like mine. most of them are about date from DOB to today and not in the format im using.

編輯:請看看到目前為止我沒有。其他線程都不像我的。他們中的大多數是關於從DOB到今天的日期,而不是我使用的格式。

2 个解决方案

#1


0  

Try this:

public class Age {

public static void main(String[] args) {
        Calendar birthDate = new GregorianCalendar(1979, 1, 1); 
        Calendar deathDate = new GregorianCalendar(2011, 1, 1);
        int age = deathDate.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR);
        if ((birthDate.get(Calendar.MONTH) > deathDate.get(Calendar.MONTH))
            || (birthDate.get(Calendar.MONTH) == deathDate.get(Calendar.MONTH) && birthDate.get(Calendar.DAY_OF_MONTH) > deathDate
                .get(Calendar.DAY_OF_MONTH))) {
          age--;
        }
        System.out.println(age);
      }

}

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复