我的目的是获取两个日期范围之间的周数。日期 24 日落在第 34 周,第 262018-08-22T12:18:06,166日落在第 35 周。现在的问题是,如果我将其作为开始日期,我将得到 34、35、36。我不期待 36 在这里,因为结束日期不属于第 36 周。任何人都可以帮助我。这个问题与此处提供的解决方案不同,从开始日期到结束日期 Java 的周数。解决方案有一个我最近发现的问题
下面是获取它的代码:
public static void main(String[] args) {
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss','SSS");
LocalDateTime startDate = LocalDateTime.parse("2018-08-24T12:18:06,166", format);
LocalDateTime endDate = LocalDateTime.parse("2018-08-26T12:19:06,188", format);
numberOfWeeks(startDate, endDate);
}
public static void numberOfWeeks(LocalDateTime startDate, LocalDateTime endDate) {
int addWeek = 0;
TemporalField tf = WeekFields.SUNDAY_START.weekOfYear();
if (startDate.get(tf) < endDate.get(tf)) {
addWeek = 1;
}
long weeks = WEEKS.between(startDate, endDate) + addWeek;
List<String> numberWeeks = new ArrayList<>();
if (weeks >= 0) {
int week = 0;
do {
//Get the number of week
LocalDateTime dt = startDate.plusWeeks(week);
int weekNumber = dt.get(tf);
numberWeeks.add(String.format("%d-W%d", dt.getYear(), weekNumber));
week++;
} while (week <= weeks);
}
System.out.println(numberWeeks);
}
侃侃尔雅
慕田峪7331174
相关分类