这就是我所做的,但没有考虑到闰年
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;
}
我听说我们必须用Joda时间计算年龄,但似乎GWT不支持Joda时间。
我们可以只使用简单的代码来计算GWT中的年龄而不是使用库吗?
注意:这是GWT问题,而不是Java问题。 GWT不支持java.text或Joda 。
答案 0 :(得分:1)
这应该有效(即使对于GWT),至少对于大约1582年后的出生:
// currentDate and birthDate are in the format yyyyMMdd
final int age = (Integer.valueOf(currentDate) - Integer.valueOf(birthDate)) / 10000;
答案 1 :(得分:1)
您已经计算了以毫秒为单位的年龄,但您需要将其转换为高度随意的Western"年。"
最好使用像JodaTime或Calendar这样的库来编写这些边缘情况。幸运的是人们把它移植到了GWT。
更新:这实际上取决于您想要的答案。通常,将当前年份与出生年份进行比较可以得到正确的答案。所以,如果你想要到目前为止的整数年:
Date dateBirth = ...;
Date dateNow = new Date();
int yearsOld = dateNow.getYear() - dateBirth.getYear();
要考虑小数年份,您需要测试闰日(如果您想要日精度)或闰秒(如果您想要闰秒精度)。所以,再次,这取决于你寻求的结果,你没有说过。
无论如何,这与GWT无关,而且与日期/时间数学的基础知识有关。要么你必须通过图书馆把它带进来,要么你自己做。我建议使用一个库,因为计算很精细。只需查看Java使用的BaseCalendar。
答案 2 :(得分:0)
我今天碰到了这个,这就是我想出来的。这模拟了我如何根据出生日期精神计算某人的年龄。 (使用com.google.gwt.i18n.shared.DateTimeFormat)
HTTP/1.1 200 OK
Date: Wed, 19 Apr 2017 22:13:15 GMT
Server: Apache
X-Frame-Options: SAMEORIGIN
Set-Cookie: SD_FRAMEWORK_SESSION=10ktfgcedl5rq427kvmo0sj7v4; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: user=abca7451088; expires=Sat, 20-May-2017 22:13:15 GMT; Max-Age=2678400; httponly
Set-Cookie: password=d30b0cc02736cf891cfca885ff5245bd; expires=Sat, 20-May-2017 22:13:15 GMT; Max-Age=2678400; httponly
Content-Length: 471
Connection: close
Content-Type: application/json; charset=UTF-8
{"special":{"login":true,"tracking":"\n<script type=\"text\/javascript\" src=\"http:\/\/url.com\/functions.js\"><\/script>\n<script type=\"text\/javascript\">\nsetPixel ({\n'location':'SIGNUP',\n'product':'test',\n'language':'cz',\n'server-id':'lobby',\n'user-id':'340668',\n'user-email':'abca30132611@mail.col',\n'user-name':'abca7451088'\n});\n<\/script>"},"messages":{"error":[],"warning":[],"success":["V\u00edt\u00e1me V\u00e1s, abca7451088"]}}
答案 3 :(得分:-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"));
}
}