Java code for calculating the number of nights in the date range (00:00:00)

I'm trying to write a Java block to find noon in a specific date range

For example:

Start date: 05 / 01 / 2014 00:00:00 end date: 05 / 03 / 2014 00:00:00

There are three noon in this range

or

Start date: 05 / 01 / 2014 00:00:00 end date: 05 / 02 / 2014 23:59:59

There is only one

It basically tells me how many times the time "00:00:00" has occurred within the date range Please help me I tried many methods, but none of them was right

Solution

The answer using joda time is incorrect Because @ khriskooper has noticed the number of noon between

2014-05-01 00:00:00 and 2014-05-02 23:59:59

Not one, but two noon!

So joda time is used for calibration here (untested), but it can also be any other library that supports schedule calculation (not applicable to the old java-pre8) I omitted the time zone detail because I didn't think it was really relevant to the problem If the op wants to, he can replace the local datetime with datetime and apply the time zone

LocalDateTime ldt1 = new LocalDateTime(2014,5,1,0);
LocalDateTime ldt2 = new LocalDateTime(2014,2,23,59,59);

int days = Days.daysBetween(ldt1.toLocalDate(),ldt2.toLocalDate()).getDays();
if (ldt1.toLocalTime().equals(new LocalTime(0,0))) {
  days++;
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>