Monday, September 29, 2014

How to calculate ages correctly in GWT?

This is what I did, but it did not take into account the leap years

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;  }  

I heard we have to use Joda time to calculate ages, but seem GWT does not support Joda time.

Can we just simply use simple code to calculate ages in GWT rather than using library?

You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
For more options, visit

No comments:

Post a Comment