2

我有两个 Date 对象,开始和结束。我想每周对它们进行迭代,即如果两者之间恰好有 4 周(日历周,而不仅仅是彼此之后的 7 天),我想要 4 次迭代,并且在每次迭代中我想获得实际的开始和结束日期.

我目前正在为此目的开发一个 Iterable,但我在想它是否可以通过 Joda Time 或智能自定义方法轻松实现。提前致谢!

编辑:我必须重申,我需要像日历一样的几周,而不是相隔 7 天。如果我的开始日期是一周中的任意一天(例如星期五),我的第一次迭代应该包含 [friday,sunday] 而不是 [friday,friday+7 days]。解决方案作为答案发布。

4

3 回答 3

1

在 Java 9 中有一个不错的LocalDate.datesUntil方法。还有一种简单的方法可以使用TemporalAdjusters在一周内调整时间。

    int numOfWeeks = 4;

    // First iteration with alignment
    LocalDate start = LocalDate.now();
    LocalDate endOfTheFirstWeek = start.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
    out.println(String.format("[%s,%s]", start, endOfTheFirstWeek));

    // Remaining iterations
    endOfTheFirstWeek
            .datesUntil(endOfTheFirstWeek.plusWeeks(numOfWeeks - 1), Period.ofWeeks(1))
            .forEach(it -> {
                LocalDate from = it.plusDays(1);
                LocalDate to = from.plusWeeks(1);
                out.println(String.format("[%s,%s]", from, to));
            });
于 2017-04-14T10:53:16.740 回答
0

要使用日历和 WEEK_OF_YEAR 进行计算:

 int startWeek;
 int finishWeek;
 int diff;
 SimpleDateFormat sdf;
 Calendar cal;
 Calendar startCountingCal;
 Date startDate;
 Date finishDate;
 String startDateS = "01/01/2013";
 String finishDateS = "01/05/2013";

 sdf = new SimpleDateFormat("dd/MM/yyyy");

 startDate = sdf.parse(startDateS);
 finishDate = sdf.parse(finishDateS);

 cal = Calendar.getInstance();

 cal.setTime(startDate);
 startWeek = cal.get(Calendar.WEEK_OF_YEAR);    

 cal.setTime(finishDate);
 finishWeek = cal.get(Calendar.WEEK_OF_YEAR);

 diff = finishWeek - startWeek;

 startCountingCal = Calendar.getInstance();
 startCountingCal.setTime(startDate);   

 for (int i = 0; i < diff; i++) {

     if (i==0) {
        System.out.println("WEEK " + i + " start: " + sdf.format(startCountingCal.getTime()));
        startCountingCal.add(Calendar.DATE, 7);
        System.out.println("WEEK " + i + " start: " + sdf.format(startCountingCal.getTime()));  
    } else {
        System.out.println("WEEK " + i + " start: " + sdf.format(startCountingCal.getTime()));
        startCountingCal.add(Calendar.DATE, 7);
        System.out.println("WEEK " + i + " start: " + sdf.format(startCountingCal.getTime()));  
    }

}

输出:

WEEK 0 start: 01/01/2013
WEEK 0 start: 08/01/2013
WEEK 1 start: 08/01/2013
WEEK 1 start: 15/01/2013
WEEK 2 start: 15/01/2013
WEEK 2 start: 22/01/2013
WEEK 3 start: 22/01/2013
WEEK 3 start: 29/01/2013
....
etc.

希望能帮助到你!

于 2013-08-28T12:47:57.233 回答
0

谢谢大家,这是一个满足我要求的工作解决方案(请注意,在大多数情况下,将 7 天添加到某一天不会产生日历周,只有当它是星期一时!)此代码使用 Joda Time API。

private static List<Interval> getWeeks(final DateTime start, final DateTime finish)
{
    final int weekLeaps = Weeks.weeksBetween(start, finish).getWeeks();
    if (weekLeaps == 0) {
        return ImmutableList.of(new Interval(start, finish));
    } else {
        // We hop sundays at least once
        final ImmutableList.Builder<Interval> resBuild = ImmutableList.<Interval> builder();

        // First week
        DateTime wStart = start;
        DateTime wEnd = rollToWeekEnd(start);
        resBuild.add(new Interval(wStart, wEnd));

        // Other weeks
        for (int i = 0; i < weekLeaps; i++) {
            wStart = wEnd.plusSeconds(1);
            DateTime actWeekEnd = rollToWeekEnd(wStart);
            wEnd = actWeekEnd.isBefore(finish) ? actWeekEnd : finish;
            resBuild.add(new Interval(wStart, wEnd));
        }

        return resBuild.build();
    }
}

private static DateTime rollToWeekEnd(final DateTime from)
{
    return from.withDayOfWeek(DateTimeConstants.SUNDAY)
        .withHourOfDay(23)
        .withMinuteOfHour(59)
        .withSecondOfMinute(59)
        .withMillisOfSecond(0);
}
于 2013-08-28T14:58:53.050 回答