本地日期对象之间的天差 [英] Days difference between local date objects
问题描述
LocalDate today=LocalDate.now();
活动日期为: eventDate = LocalDate.of(year,monthOfYear,dayOfMonth);
(来自日期选择器对话框)
And the event date is:
eventDate=LocalDate.of(year, monthOfYear,dayOfMonth);
(from the date picker dialog)
我正在尝试计算它们之间的天差...我发现的最短的东西是:
I'm trying to calculate the days difference between them... The shortest thing I have found is this:
int DaysDifference = Period.between(eventToDisplay.getEventDate(),today).getDays();
第一个对象是"today",第二个对象是"eventToDisplay.getEventDate()".它对我不起作用,它显示了错误的天数.
While the first object is "today", and the second one is "eventToDisplay.getEventDate()." It didn't work for me, it showed the wrong number of days.
我也试图这样做:
eventToDisplay.getEventDate().compareTo(today)
也没有工作...我还尝试了不使用joda-time的方法,因为我遇到了麻烦,因为我想用日期和时间来做...
Also didn't work... I have also tried to do it without joda-time, because I had troubles with it, because of what I'm trying to do with date and time...
我发现的其他事情又漫长又复杂,我认为也许有一种更好的方法,而无需手动操作.
The other things I have found are long and complicated, and I thought maybe there is a better way, without the joda-time.
我刚刚尝试过:
Calendar now = Calendar.getInstance();
Calendar chosenDate=Calendar.getInstance();
chosenDate.set(eventToDisplay.getEventDate().getYear(),eventToDisplay.getEventDate().getMonth().getValue(),eventToDisplay.getEventDate().getDayOfMonth());
long def= chosenDate.getTimeInMillis() - now.getTimeInMillis();
long DaysDifference =TimeUnit.MILLISECONDS.toDays(def);
不是为我工作
这对我有用:
LocalDate today=LocalDate.now();
Calendar now = Calendar.getInstance();
now.set(today.getYear(),today.getMonthValue(),today.getDayOfMonth());
Calendar chosenDate=Calendar.getInstance();
chosenDate.set(eventToDisplay.getEventDate().getYear(),eventToDisplay.getEventDate().getMonthValue(),eventToDisplay.getEventDate().getDayOfMonth());
long def= chosenDate.getTimeInMillis() - now.getTimeInMillis();
long daysDifference =TimeUnit.MILLISECONDS.toDays(def);
推荐答案
您可以使用以下内容:
Calendar now = Calendar.getInstance();
Calendar end=Calendar.getInstance();
end.set(<year>, <month>, <day>);
long def= end.getTimeInMillis() - now.getTimeInMillis();
long days =TimeUnit.MILLISECONDS.toDays(def);
这篇关于本地日期对象之间的天差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!