可能重複:
How do I calculate someone's age in Java?如何計算從出生日期到當前日期的當前年齡?
喜的朋友
我想計算從生日任何註冊人的總年齡爲當前日期...我要算總年數以及在生日和當前日期之間的月和日。..
例如= birthdate = 8th feb 2008 的currentdate = 8 april2011 ,所以我想答案是= 3年,1月
可能重複:
How do I calculate someone's age in Java?如何計算從出生日期到當前日期的當前年齡?
喜的朋友
我想計算從生日任何註冊人的總年齡爲當前日期...我要算總年數以及在生日和當前日期之間的月和日。..
例如= birthdate = 8th feb 2008 的currentdate = 8 april2011 ,所以我想答案是= 3年,1月
看一看http://joda-time.sourceforge.net/ - 它有一個相當不錯的API。我在另外一週使用它來做日期的一些工作。希望它會有你需要的。
下面是使用http://joda-time.sourceforge.net/
import org.joda.time.DateTime;
import org.joda.time.Period;
public class Main{
public static void main(String[] args) {
DateTime start = new DateTime(2008, 2, 8, 0, 0, 0, 0);
DateTime end = new DateTime();
Period period = new Period(start, end);
System.out.println(" user is " + period.getYears() + " years " + period.getMonths() + " months old");
}
}
thx爲您解答:) – SacreDeveloper 2014-07-07 08:54:25
誘惑關閉爲[問題#9](http://stackoverflow.com/questions/9/how-do-i-calculate-someones-的重複的實例這是C#,這是Java = p – 2011-04-08 13:24:11
告訴我邏輯簡單yar ... – 2011-04-08 13:25:20
邏輯並不簡單,因爲每個月的天數不同。按照建議使用庫是最容易的。 – roberttdev 2011-04-08 13:45:27