两个日期之间的Andr​​oid天日期、两个、oid、Andr

2023-09-07 14:38:49 作者:一绾青颜,缓我挂念

我要比较两个日期我的Andr​​oid应用程序,但我有一个非常奇怪的问题。

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

例如:

如果我设置了背过去日期为127天前:

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;

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

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 ?

推荐答案

请参阅此code,这可能会帮助你。

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