(Java / Android) 计算两个日期之间的天数并以特定格式显示结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10325117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
(Java / Android) Calculate days between 2 dates and present the result in a specific format
提问by striders
I am trying to do calculate days between 2 dates as follow:
我正在尝试计算两个日期之间的天数,如下所示:
- Obtain current date
- Obtain past OR future date
- Calculate the difference between no. 1 and no. 2
- 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
- 获取当前日期
- 获取过去或未来日期
- 计算 no 之间的差异。1 和没有。2
- 按以下格式显示日期
- 如果结果是过去(2 天前)或将来(2 天后)
- 格式将:天、周、月、年
I tried different ways but couldn't get the result I wanted above. I found out that Android DatePickerdialog box convert date into Integer.I have not found a way to make DatePicket widget to return date variables instead of integer.
我尝试了不同的方法,但无法得到上面想要的结果。我发现Android DatePicker对话框将日期转换为整数。我还没有找到一种方法让 DatePicket 小部件返回日期变量而不是整数。
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.
我试图根据上述内容将系统日期转换为整数,但是在尝试计算 2 个日期之间的天数时,这并没有真正起作用。
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;
}
回答by San
/**
* 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
编辑:刚刚在另一个线程中看到了同样的问题: 如何使用 java 计算两个日期之间的差异
Edit2: To get Year/Month/Week, do something like this:
Edit2:要获得年/月/周,请执行以下操作:
int year = delta / 365;
int rest = delta % 365;
int month = rest / 30;
rest = rest % 30;
int weeks = rest / 7;
int days = rest % 7;
回答by ahodder
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.
从这里,您只需选择您的格式。也许使用日期格式化程序?至于确定未来的东西等等,你可以看看delta是正还是负。负 delta 表示过去的事件。
回答by AndroidCoolestRulest
Well, java/android API has all answers for you:
好吧,java/android API 为您提供了所有答案:
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);