两个日期之间的Android天数

两个日期之间的Android天数,第1张

两个日期之间的Android天数

请参考此代码,这可能会对您有所帮助。

public String getCountOfDays(String createdDateString, String expireDateString) {    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());    Date createdConvertedDate = null, expireCovertedDate = null, todayWithZeroTime = null;    try {        createdConvertedDate = dateFormat.parse(createdDateString);        expireCovertedDate = dateFormat.parse(expireDateString);        Date today = new Date();        todayWithZeroTime = dateFormat.parse(dateFormat.format(today));    } catch (ParseException e) {        e.printStackTrace();    }    int cYear = 0, cMonth = 0, cDay = 0;    if (createdConvertedDate.after(todayWithZeroTime)) {        Calendar cCal = Calendar.getInstance();        cCal.setTime(createdConvertedDate);        cYear = cCal.get(Calendar.YEAR);        cMonth = cCal.get(Calendar.MONTH);        cDay = cCal.get(Calendar.DAY_OF_MONTH);    } else {        Calendar cCal = Calendar.getInstance();        cCal.setTime(todayWithZeroTime);        cYear = cCal.get(Calendar.YEAR);        cMonth = cCal.get(Calendar.MONTH);        cDay = cCal.get(Calendar.DAY_OF_MONTH);    }        Calendar eCal = Calendar.getInstance();    eCal.setTime(expireCovertedDate);    int eYear = eCal.get(Calendar.YEAR);    int eMonth = eCal.get(Calendar.MONTH);    int eDay = eCal.get(Calendar.DAY_OF_MONTH);    Calendar date1 = Calendar.getInstance();    Calendar date2 = Calendar.getInstance();    date1.clear();    date1.set(cYear, cMonth, cDay);    date2.clear();    date2.set(eYear, eMonth, eDay);    long diff = date2.getTimeInMillis() - date1.getTimeInMillis();    float dayCount = (float) diff / (24 * 60 * 60 * 1000);    return ("" + (int) dayCount + " Days");}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5129831.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存