Android两天之间的天数 [英] Android Days between two dates

查看:209
本文介绍了Android两天之间的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较Android应用程式的两个日期,但我有一个很奇怪的问题。



例如:



如果我将在过去日期设置为127天前:

  this.dateEvent = System.currentTimeMillis() - (127 * 24 * 3600 * 1000)


$ b b

然后将其与当前日期(天之间)进行比较

 日历sDate = getDatePart dateEvent)); 
Calendar eDate = getDatePart(new Date(System.currentTimeMillis()));

int daysBetween = 0;
while(sDate.before(eDate))
{
sDate.add(Calendar.DAY_OF_MONTH,1);
天Between++;
}

while(sDate.after(eDate))
{
eDate.add(Calendar.DAY_OF_MONTH,1);
天Between++;
}

return daysBetween;

它将返回22,这根本不是预期的。



我做错了或者是日历类的问题?

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

今天日期= 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);
* /

日历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);

日历date1 = Calendar.getInstance();
日历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);
}


I want to compare two dates for my Android application, but I got a really weird issue.

For example:

If I set the back in the past date to 127 days ago:

this.dateEvent = System.currentTimeMillis() - (127 * 24 * 3600 * 1000)

And then compare it to the current date (Days between)

    Calendar sDate = getDatePart(new Date(this.dateEvent));
    Calendar eDate = getDatePart(new Date(System.currentTimeMillis()));

    int daysBetween = 0;
    while (sDate.before(eDate))
    {
        sDate.add(Calendar.DAY_OF_MONTH, 1);
        daysBetween ++;
    }

    while (sDate.after(eDate))
    {
        eDate.add(Calendar.DAY_OF_MONTH, 1);
        daysBetween ++;
    }

    return daysBetween;

It will return 22 which is not at all what was expected.

Did I make something wrong or is that an issue with the Calendar class ?

解决方案

Please refer this code, this may help you.

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

这篇关于Android两天之间的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆