两个日期之间无法获得年份

时间:2017-07-03 18:17:09

标签: android date datediff

我希望两个日期之间有年差异。其中一个日期是当前日期和时间。另一个是用户的出生日期。

我可以正确地获取用户的出生日期,例如:1995-06-18T19:36:00.000Z

我收到了这个错误:

  

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'int java.lang.String.length()'

这是我的功能:

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

4 个答案:

答案 0 :(得分:2)

首先,您不需要这样做:

Date date1 = sdf.parse(sdf.format(cal.getTime()));

只是做:

Date date1 = cal.getTime();

要计算年龄,请将date1放在date2之前,否则您将有一个负值:

userAge = (date1.getTime() - date2.getTime()) / 86400000;

使用comingDate作为1995-06-18T19:36:00.000Z,我得到了结果8050(相当于22年),所以它似乎是正确的。

如果您希望年龄而不是几天:

userAge = (date1.getTime() - date2.getTime()) / 86400000 / 365;

结果为22

新Java日期/时间API

如果您对添加Android项目的依赖关系不错,可以使用ThreeTen Backport,这是Java 8的新日期/时间类的优秀后端,以及{{ 3}}(更多关于如何使用它ThreeTenABP)。

所有类都在org.threeten.bp包中。代码更简单

import org.threeten.bp.temporal.ChronoUnit;
import org.threeten.bp.Instant;
import org.threeten.bp.ZonedDateTime;

String comingDate = "1995-06-18T19:36:00.000Z";
// get the number of days between comingDate and current date (result is 8050)
int userAge = (int) ChronoUnit.DAYS.between(Instant.parse(comingDate), Instant.now()); // 8050
// get the age in years (result is 22)
userAge = (int) ChronoUnit.YEARS.between(ZonedDateTime.parse(comingDate), ZonedDateTime.now()); // 22

要获得年龄,我必须使用ZonedDateTime,因为有些单位不能与Instant合作。

答案 1 :(得分:1)

很简单:

  int daysDifference = (date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000);

Date.getTime()获取自" 1970年1月1日"以来的时间(以毫秒为单位)那么我们需要在一天内除以毫秒。

<强> EDITED 并且您不需要日历来获取实际时间,新的Date()。getTime();返回它

    int daysPassedSinceADate =  (new Date().getTime() - date.getTime()) / (24 * 60 * 60 * 1000);

如果您的应用使用了这么多日期或时间,请不要将其保存为字符串,请将其保存为更长,更简单。

答案 2 :(得分:0)

这将有助于您

 public String get_count_of_days(String Created_date_String, String 
    Expire_date_String) 

 {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", 
Locale.getDefault());

Date Created_convertedDate = null, Expire_CovertedDate = null, todayWithZeroTime = null;
try {
    Created_convertedDate = dateFormat.parse(Created_date_String);
    Expire_CovertedDate = dateFormat.parse(Expire_date_String);

    Date today = new Date();

    todayWithZeroTime = dateFormat.parse(dateFormat.format(today));
} catch (ParseException e) {
    e.printStackTrace();
}

int c_year = 0, c_month = 0, c_day = 0;

if (Created_convertedDate.after(todayWithZeroTime)) {
    Calendar c_cal = Calendar.getInstance();
    c_cal.setTime(Created_convertedDate);
    c_year = c_cal.get(Calendar.YEAR);
    c_month = c_cal.get(Calendar.MONTH);
    c_day = c_cal.get(Calendar.DAY_OF_MONTH);

} else {
    Calendar c_cal = Calendar.getInstance();
    c_cal.setTime(todayWithZeroTime);
    c_year = c_cal.get(Calendar.YEAR);
    c_month = c_cal.get(Calendar.MONTH);
    c_day = c_cal.get(Calendar.DAY_OF_MONTH);
}


/*Calendar today_cal = Calendar.getInstance();
int today_year = today_cal.get(Calendar.YEAR);
int today = today_cal.get(Calendar.MONTH);
int today_day = today_cal.get(Calendar.DAY_OF_MONTH);
*/

Calendar e_cal = Calendar.getInstance();
e_cal.setTime(Expire_CovertedDate);

int e_year = e_cal.get(Calendar.YEAR);
int e_month = e_cal.get(Calendar.MONTH);
int e_day = e_cal.get(Calendar.DAY_OF_MONTH);

Calendar date1 = Calendar.getInstance();
Calendar date2 = Calendar.getInstance();

date1.clear();
date1.set(c_year, c_month, c_day);
date2.clear();
date2.set(e_year, e_month, e_day);

long diff = date2.getTimeInMillis() - date1.getTimeInMillis();

float dayCount = (float) diff / (24 * 60 * 60 * 1000);

return ("" + (int) dayCount + " Days");
}

答案 3 :(得分:0)

public long getDifferenceBetweenDates(Date dateOfBirth, Date currentDate) {
    long difference = currentDate.getTime() - dateOfBirth.getTime();
    long daysInMilliseconds = 60 * 60 * 24 * 1000;
    long elapsedDays = difference / daysInMilliseconds;
    return elapsedDays;
}