2014-09-30 56 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; 
}