2014-09-30 22 views
1

這是我做的,但它並沒有考慮到閏年如何在GWT中正確計算年齡?

public static int calculateAge(String yyyyMMddBirthDate){ 
    DateTimeFormat fmt = DateTimeFormat.getFormat("yyyy-MM-dd"); 
    Date birthDateDate = fmt.parse(yyyyMMddBirthDate); 
    Date nowDate=new Date(); 
    long differenceInMillis=nowDate.getTime()-birthDateDate.getTime(); 
    int days=(int)Math.abs(differenceInMillis/(1000*60*60*24)); 
    int ages=(int)days/365; 
    System.out.println(days+" days"); 


    return ages; 
} 

聽說我們要使用喬達時間來計算年齡,但似乎GWT不支持喬達時間。

我們可以簡單地使用簡單的代碼來計算GWT中的年齡,而不是使用庫嗎?

注:這是GWT問題,而不是Java問題。 GWT不支持java.text或Joda

+0

可能重複[我如何計算在Java中的人的年齡?(http://stackoverflow.com/questions/1116123/how-我在計算 - someones-age-in-java) – 2014-09-30 03:48:12

+0

@Jarrod,它需要Joda庫&我沒有在我的GWT中,我不喜歡使用它。爲什麼我們需要關閉這個問題?這是gwt而不是Java – Tum 2014-09-30 03:52:28

+1

@JarrodRoberson這個問題不是重複的http://stackoverflow.com/questions/1116123/how-do-i-calculate-someones-age-ja-java – 2014-09-30 04:08:27

回答

-2
import java.util.*; 

public class Practice2 { 
    public static int calculateAge(String yyyyMMddBirthDate) { 
     // check the input ?    
     int birthYear = Integer.parseInt(yyyyMMddBirthDate.substring(0,4)); 
     Date nowDate = new Date(); 
     int nowYear = nowDate.getYear() + 1900; 

     //rough age and check whether need -- below 
     int roughAge = nowYear - birthYear; 

     int birthMonth = Integer.parseInt(yyyyMMddBirthDate.substring(5,7)); 
     int birthDay = Integer.parseInt(yyyyMMddBirthDate.substring(8,10)); 

     //1970-01-01T 00:00:00 
     long now = nowDate.getTime(); 
     long days = now/(3600l * 1000l * 24l); 
     int daySum = 0; 
     for(int i = 1970; i < nowYear; i ++ ){ 
      daySum = daySum + yearDays(i); 
     } 
     int[] monthDays = {31, 28 , 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 
     monthDays[1] = yearDays(nowYear) - 337; 
     for(int i = 0; i < birthMonth -1; i ++){ 
      daySum = daySum + monthDays[i]; 
     } 
     daySum = daySum + birthDay; 


     if( daySum > days){ 
      roughAge--; 
     } 

     return roughAge; 
    } 

    private static int yearDays(int year) { 
     if(((year%100==0)&&(year%400==0)) 
       ||((year%100!=0)&&(year%4==0))){ 
      return 366; 
     } 
     return 365; 
    } 

    public static void main(String[] args) { 
     System.out.println(calculateAge("1990-12-30")); 
    } 
} 
+3

GWT獨木舟不支持Java。文本&因此不能使用SimpleDateFormat或日曆 – Tum 2014-09-30 04:18:55

+0

@tum如果您考慮閏日,新的更新代碼將會有所幫助。 – 2014-10-03 03:17:19

1

這應該工作(即使是GWT),至少對於那些經過約1582胞胎:

// currentDate and birthDate are in the format yyyyMMdd 
final int age = (Integer.valueOf(currentDate) - Integer.valueOf(birthDate))/10000; 
1

你已經計算以毫秒爲單位的年齡,但你需要轉換它以高度隨意的西方「年代」。

最好使用像JodaTime或Calendar這樣的庫,它們已經編程了這些邊緣情況。幸運的是,人們已經將它移植到GWT中。

參見:Date time library for gwt

更新:這真的取決於你想要什麼答案。一般來說,將當年與出生年進行比較將爲您提供正確的答案。所以,如果你想整年住至今:

Date dateBirth = ...; 
Date dateNow = new Date(); 
int yearsOld = dateNow.getYear() - dateBirth.getYear(); 

要佔分數的一年,你將需要測試的飛躍天(如果你想一天精密)或閏秒(如果你想跳秒精確)。所以,這又取決於你所尋求的結果,你沒有說過。

無論如何,這與GWT和與日期/時間數學基礎知識無關。要麼你必須通過圖書館進入,要麼自己做。我會建議一個圖書館,因爲計算是精心製作的。看看Java使用的BaseCalendar

+0

對於一個簡單的函數來說太複雜了,你能想出一個非常簡單的方法嗎? – Tum 2014-09-30 14:00:01

+0

@Tum看到更新的答案 – 2014-09-30 14:59:37

+0

getYears已被棄用,但由於我們沒有在GWT中的日曆所以它可以,但是int yearsOld = dateNow.getYear() - dateBirth.getYear();只計算幾年,它不如毫秒精確。例如,今天是10ct14,生日是1977年10月11日,那麼它應該顯示36,但它變成37,(正確的是36年和8個月 – Tum 2014-09-30 15:38:42

0

我今天遇到了這個,這是我想出來的。這模擬瞭如何根據出生日期在思維上計算某個人的年齡。 (使用com.google.gwt.i18n.shared.DateTimeFormat)的

private int getAge(Date birthDate) { 
    int birthYear = Integer.parseInt(DateTimeFormat.getFormat("yyyy").format(birthDate)); 
    int birthMonth = Integer.parseInt(DateTimeFormat.getFormat("MM").format(birthDate)); 
    int birthDayOfMonth = Integer.parseInt(DateTimeFormat.getFormat("dd").format(birthDate)); 
    Date today = new Date(); 
    int todayYear = Integer.parseInt(DateTimeFormat.getFormat("yyyy").format(today)); 
    int todayMonth = Integer.parseInt(DateTimeFormat.getFormat("MM").format(today)); 
    int todayDayOfMonth = Integer.parseInt(DateTimeFormat.getFormat("dd").format(today)); 

    boolean hasHadBirthdayThisYear; 
    if (todayMonth < birthMonth) { 
     hasHadBirthdayThisYear = false; 
    } else if (todayMonth > birthMonth) { 
     hasHadBirthdayThisYear = true; 
    } else { 
     hasHadBirthdayThisYear = birthDayOfMonth <= todayDayOfMonth; 
    } 

    int age = todayYear - birthYear; 
    if (!hasHadBirthdayThisYear) { 
     age--; 
    } 
    return age; 
}