如2015-12-01到2016-01-15隔了5周
if (oneday.isAfter(sratDate) && oneday.isBefore(endDate)) {
TemporalField weekFields =WeekFields.of(Locale.getDefault()).weekOfWeekBasedYear();
int startWeek = sratDate.get(weekFields);
int oneWeek = oneday.get(weekFields);
System.out.print(startWeek - oneWeek);
}
================================ September 2016 version dividing line ========= ===============================
The previous version of me was too simple... I recently studied it again Java8’s new time API, find a simpler way to handle it
That's it... Use the
Temporal.unitl
method. This method is to calculate the distance of theTemporal
unit type between twoTemporalUnit
s.LocalDate
is also an implementation class ofTemporal
, so it can When called, the unitChronoUnit
is an enumeration, which has many units to choose from, and can calculate other time distances(p.s: Java8's time API abstracts the concept of time, uses
Temporal
andTemporalAccessor
to define the cornerstone of time, and abstracts the time relationship related to the cornerstone, time unitTemporalUnit
, time fieldTemporalField
, amount of timeTemporalAmount
, straighten out these abstract interfaces... You will find that using Java8 time API can satisfy almost any time calculation you have. The above is my personal understanding and is for reference only)==============================November 2015 version dividing line========== ==============================
I think the week number is not used much in the new API of java8. It basically counts years, months, and days. Here is a method to calculate the number of years, months, and days between two LocalDates. See if it can be of some help to you
This Period object encapsulates some time data between two LocalDates. This result indicates that the current difference between the two times is 1 month and 14 days