LocalDate startDate = LocalDate.of(2019, 12, 12).with(DayOfWeek.MONDAY);
LocalDate endDate = LocalDate.of(2020, 12, 12);
//number of weeks
long weekNumber = ChronoUnit.WEEKS.between(startDate, endDate);
Map<LocalDate, LocalDate> weeks = new LinkedHashMap<>();
for (int i = 0; i < weekNumber; i++) {
LocalDate endOfWeek = startDate.plusDays(6);
weeks.put(startDate, endOfWeek);
startDate = endOfWeek.plusDays(1);
}
for (LocalDate week : weeks.keySet()) {
LocalDate endOfWeek = weeks.get(week);
System.out.println("Start of week: " + week + "[" + week.getDayOfWeek() + "]" +
" End of week: " + endOfWeek + "[" + endOfWeek.getDayOfWeek() + "]");
}
它打印:
Start of week: 2020-12-07[MONDAY] End of week: 2020-12-13[SUNDAY]
Start of week: 2020-12-14[MONDAY] End of week: 2020-12-20[SUNDAY]
Start of week: 2020-12-21[MONDAY] End of week: 2020-12-27[SUNDAY]
Start of week: 2020-12-28[MONDAY] End of week: 2021-01-03[SUNDAY]
Start of week: 2021-01-04[MONDAY] End of week: 2021-01-10[SUNDAY]
等。。。 编辑: 使用datetimeformatter,日期将更短,日期名称将以您的语言显示:
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd[EEEE]");
for (LocalDate startOfWeek : weeks.keySet()) {
LocalDate endOfWeek = weeks.get(startOfWeek);
System.out.println("Start of week: " + dateTimeFormatter.format(startOfWeek) +
" End of week: " + dateTimeFormatter.format(endOfWeek));
}
static void test(int startYear, int startMonth, int startDay, int endYear, int endMonth, int endDay, Locale locale) {
Calendar start = Calendar.getInstance(locale);
Calendar end = Calendar.getInstance(locale);
start.clear();
end.clear();
start.set(startYear, startMonth - 1, startDay);
end.set(endYear, endMonth - 1, endDay);
List<DateRange> weeks = weeksCovering(start, end);
System.out.println(new DateRange(start, end) + ":");
for (DateRange week : weeks)
System.out.println(" " + week);
}
输出
Sun, Dec 13, 2020 - Sun, Feb 7, 2021:
Sun, Dec 13, 2020 - Sat, Dec 19, 2020
Sun, Dec 20, 2020 - Sat, Dec 26, 2020
Sun, Dec 27, 2020 - Sat, Jan 2, 2021
Sun, Jan 3, 2021 - Sat, Jan 9, 2021
Sun, Jan 10, 2021 - Sat, Jan 16, 2021
Sun, Jan 17, 2021 - Sat, Jan 23, 2021
Sun, Jan 24, 2021 - Sat, Jan 30, 2021
Sun, Jan 31, 2021 - Sat, Feb 6, 2021
Sun, Feb 7, 2021 - Sat, Feb 13, 2021
Sun, Dec 13, 2020 - Sun, Feb 7, 2021:
Mon, Dec 7, 2020 - Sun, Dec 13, 2020
Mon, Dec 14, 2020 - Sun, Dec 20, 2020
Mon, Dec 21, 2020 - Sun, Dec 27, 2020
Mon, Dec 28, 2020 - Sun, Jan 3, 2021
Mon, Jan 4, 2021 - Sun, Jan 10, 2021
Mon, Jan 11, 2021 - Sun, Jan 17, 2021
Mon, Jan 18, 2021 - Sun, Jan 24, 2021
Mon, Jan 25, 2021 - Sun, Jan 31, 2021
Mon, Feb 1, 2021 - Sun, Feb 7, 2021
注意它如何正确地调整到 Locale 由起始日历对象标识。
使用更新的Java8时间api
static List<DateRange> weeksCovering(LocalDate start, LocalDate end, WeekFields weekFields) {
List<DateRange> result = new ArrayList<>();
LocalDate date = start.with(TemporalAdjusters.previousOrSame(weekFields.getFirstDayOfWeek()));
while (date.compareTo(end) <= 0) {
LocalDate weekStart = date;
date = date.plusDays(6);
LocalDate weekEnd = date;
date = date.plusDays(1);
result.add(new DateRange(weekStart, weekEnd));
}
return result;
}
public final class DateRange {
private final LocalDate start;
private final LocalDate end;
public DateRange(LocalDate start, LocalDate end) {
this.start = start;
this.end = end;
}
public LocalDate getStart() {
return this.start;
}
public LocalDate getEnd() {
return this.end;
}
@Override
public String toString() {
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("EEE, MMM d, uuuu");
return dateFormat.format(this.start) + " - " + dateFormat.format(this.end);
}
}
static void test(int startYear, int startMonth, int startDay, int endYear, int endMonth, int endDay, Locale locale) {
LocalDate start = LocalDate.of(startYear, startMonth, startDay);
LocalDate end = LocalDate.of(endYear, endMonth, endDay);
List<DateRange> weeks = weeksCovering(start, end, WeekFields.of(locale));
System.out.println(new DateRange(start, end) + ":");
for (DateRange week : weeks)
System.out.println(" " + week);
}
输出
Sun, Dec 13, 2020 - Sun, Feb 7, 2021:
Sun, Dec 13, 2020 - Sat, Dec 19, 2020
Sun, Dec 20, 2020 - Sat, Dec 26, 2020
Sun, Dec 27, 2020 - Sat, Jan 2, 2021
Sun, Jan 3, 2021 - Sat, Jan 9, 2021
Sun, Jan 10, 2021 - Sat, Jan 16, 2021
Sun, Jan 17, 2021 - Sat, Jan 23, 2021
Sun, Jan 24, 2021 - Sat, Jan 30, 2021
Sun, Jan 31, 2021 - Sat, Feb 6, 2021
Sun, Feb 7, 2021 - Sat, Feb 13, 2021
Sun, Dec 13, 2020 - Sun, Feb 7, 2021:
Mon, Dec 7, 2020 - Sun, Dec 13, 2020
Mon, Dec 14, 2020 - Sun, Dec 20, 2020
Mon, Dec 21, 2020 - Sun, Dec 27, 2020
Mon, Dec 28, 2020 - Sun, Jan 3, 2021
Mon, Jan 4, 2021 - Sun, Jan 10, 2021
Mon, Jan 11, 2021 - Sun, Jan 17, 2021
Mon, Jan 18, 2021 - Sun, Jan 24, 2021
Mon, Jan 25, 2021 - Sun, Jan 31, 2021
Mon, Feb 1, 2021 - Sun, Feb 7, 2021
2条答案
按热度按时间z9zf31ra1#
下面是您的问题的实现,使用新的时间api,阅读localdate,localdatetime,period等。
它打印:
等。。。
编辑:
使用datetimeformatter,日期将更短,日期名称将以您的语言显示:
wbrvyc0a2#
和老人一起做
Calendar
对象,你可以像答案的前半部分那样做。要使用Java8中添加的较新的JavaTimeAPI来实现这一点,请参阅答案的后半部分。使用旧日历api
测试
输出
注意它如何正确地调整到
Locale
由起始日历对象标识。使用更新的Java8时间api
测试
输出
注意它如何正确地调整到
Locale
由起始日历对象标识。