(Java / Android) Calculate days between 2 dates and present the result in a specific format

和自甴很熟 提交于 2019-12-29 05:22:20

问题


I am trying to do calculate days between 2 dates as follow:

  1. Obtain current date
  2. Obtain past OR future date
  3. Calculate the difference between no. 1 and no. 2
  4. Present the dates in the following format
    • If the result is in past (2 day ago) or in the future (in 2 days)
    • Format will: days, weeks, months, years

I tried different ways but couldn't get the result I wanted above. I found out that Android DatePicker dialog box convert date into Integer. I have not found a way to make DatePicket widget to return date variables instead of integer.

private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, **int** year, 
                                  **int** monthOfYear, **int** dayOfMonth) {
                enteredYear = year;
                enteredMonth = monthOfYear;
                enteredDay = dayOfMonth;
            }
        };

I tried to convert the system date to Integer, based on the above, but this doesn't really work when trying to calculate days between 2 dates.

private void getSystemDate(){
     final Calendar c = Calendar.getInstance();

        mYear = c.get(Calendar.YEAR);
        systemYear = mYear;

        mMonth = c.get(Calendar.MONTH);
        systemMonth = mMonth + 1;

        mDay = c.get(Calendar.DAY_OF_MONTH);
        systemDay = mDay;

}

回答1:


/** 
  *  Returns a string that describes the number of days
  *  between dateOne and dateTwo.  
  *
  */ 

public String getDateDiffString(Date dateOne, Date dateTwo)
{
    long timeOne = dateOne.getTime();
    long timeTwo = dateTwo.getTime();
    long oneDay = 1000 * 60 * 60 * 24;
    long delta = (timeTwo - timeOne) / oneDay;

    if (delta > 0) {
        return "dateTwo is " + delta + " days after dateOne";
    }
    else {
        delta *= -1;
        return "dateTwo is " + delta + " days before dateOne";
    }
}

Edit: Just saw the same question in another thread: how to calculate difference between two dates using java

Edit2: To get Year/Month/Week, do something like this:

int year = delta / 365;
int rest = delta % 365;
int month = rest / 30;
rest = rest % 30;
int weeks = rest / 7;
int days = rest % 7;



回答2:


long delta = Date2.getTime() - Date1.getTime();

new Date(delta);

From here, you just pick your format. Maybe use a date formatter? As for determing the future stuff and all that, you could just see if the delta is positive or negative. A negative delta will indicate a past event.




回答3:


Well, java/android API has all answers for you:

    Calendar myBirthday=Calendar.getInstance();
    myBirthday.set(1980, Calendar.MARCH, 22);
    Calendar now = Calendar.getInstance();
    long diffMillis= Math.abs(now.getTimeInMillis()-myBirthday.getTimeInMillis());
    long differenceInDays = TimeUnit.DAYS.convert(diffMillis, TimeUnit.MILLISECONDS);


来源:https://stackoverflow.com/questions/10325117/java-android-calculate-days-between-2-dates-and-present-the-result-in-a-spec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!