I want to get year difference between two dates. One of these dates is the current date and time. And the other one is user's birth date.
I can get user's birth date properly, like that: 1995-06-18T19:36:00.000Z
I'm getting this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
Here is my function:
public int calculateAge(String comingDate)
{
long userAge = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+03:00"));
Calendar cal = Calendar.getInstance();
try
{
Date date1 = sdf.parse(sdf.format(cal.getTime()));
Date date2 = sdf.parse(comingDate);
userAge = (date2.getTime() - date1.getTime())/86400000;
}
catch (ParseException e)
{
e.printStackTrace();
}
return (int)userAge;
}