如何跨年计算 两日期之间相隔的周数 with java8 time API
大家讲道理
大家讲道理 2017-04-17 15:35:05
0
1
552

如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);
        }
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(1)
左手右手慢动作

================================ 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

LocalDate localDate1 = LocalDate.parse("2015-12-01");
LocalDate localDate2 = LocalDate.parse("2016-01-15");
System.out.println(localDate1.until(localDate2, ChronoUnit.WEEKS));

That's it... Use the Temporal.unitl method. This method is to calculate the distance of the Temporal unit type between two TemporalUnits. LocalDate is also an implementation class of Temporal, so it can When called, the unit ChronoUnit 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 and TemporalAccessor to define the cornerstone of time, and abstracts the time relationship related to the cornerstone, time unit TemporalUnit, time field TemporalField, amount of time TemporalAmount, 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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template