Java Calendar:计算前一周的开始日期和结束日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7742293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:13:55  来源:igfitidea点击:

Java Calendar: calculate start date and end date of the previous week

java

提问by adrift

What is most convenient and shortest way to get start and end dates of the previous week? Example: today is 2011-10-12 (input data),but I want to get 2011-10-03 (Monday's date of previous week) and 2011-10-09 (Sunday's date of previous week).

获取上周开始和结束日期的最方便和最短的方法是什么?示例:今天是 2011-10-12(输入数据),但我想得到 2011-10-03(上周的星期一日期)和 2011-10-09(上周的星期日日期)。

回答by Sean Patrick Floyd

Here's another JodaTime solution. Since you seem to want Dates only (not timestamps), I'd use the DateMidnight class:

这是另一个 JodaTime 解决方案。由于您似乎只想要日期(而不是时间戳),我将使用 DateMidnight 类:

final DateTime input = new DateTime();
System.out.println(input);
final DateMidnight startOfLastWeek = 
    new DateMidnight(input.minusWeeks(1).withDayOfWeek(DateTimeConstants.MONDAY));
System.out.println(startOfLastWeek);
final DateMidnight endOfLastWeek = startOfLastWeek.plusDays(6);
System.out.println(endOfLastWeek);

Output:

输出:

2011-10-12T18:13:50.865+02:00
2011-10-03T00:00:00.000+02:00
2011-10-10T00:00:00.000+02:00

回答by jarnbjo

public static Calendar firstDayOfLastWeek(Calendar c) {
    c = (Calendar) c.clone();
    // last week
    c.add(Calendar.WEEK_OF_YEAR, -1);
    // first day
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
    return c;
}

public static Calendar lastDayOfLastWeek(Calendar c) {
    c = (Calendar) c.clone();
    // first day of this week
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
    // last day of previous week
    c.add(Calendar.DAY_OF_MONTH, -1);
    return c;
}

回答by ZJ Lyu

I would go for @maerics answer if third party library is not involved. I have to replace roll() method with add() method as roll will leave the higher field unchanged. e.g., 22nd August will be obtained from 1st August being rolled -7 days. Note the month remain unchanged. The source code goes as below.

如果不涉及第三方库,我会去@maerics 回答。我必须用 add() 方法替换 roll() 方法,因为 roll 会使更高的字段保持不变。例如,8 月 22 日将从 8 月 1 日滚动-7 天获得。注意月份保持不变。源代码如下。

public static Calendar[] getLastWeekBounds(Calendar c) {
  int cdow = c.get(Calendar.DAY_OF_WEEK);
  Calendar lastMon = (Calendar) c.clone();
  lastMon.add(Calendar.DATE, -7 - (cdow - Calendar.MONDAY));
  Calendar lastSun = (Calendar) lastMon.clone();
  lastSun.add(Calendar.DATE, 6);
  return new Calendar[] { lastMon, lastSun };
}

回答by maerics

You can use the Calendar.roll(int,int)methodwith arguments Calendar.DATEand an offset for the current day of week:

您可以使用带有参数和当前星期几的偏移量的Calendar.roll(int,int)方法Calendar.DATE

public static Calendar[] getLastWeekBounds(Calendar c) {
  int cdow = c.get(Calendar.DAY_OF_WEEK);
  Calendar lastMon = (Calendar) c.clone();
  lastMon.roll(Calendar.DATE, -7 - (cdow - Calendar.MONDAY));
  Calendar lastSun = (Calendar) lastMon.clone();
  lastSun.roll(Calendar.DATE, 6);
  return new Calendar[] { lastMon, lastSun };
}

This function returns an array of two Calendars, the first being last week's Monday and last week's Sunday.

此函数返回包含两个日历的数组,第一个是上周的星期一和上周的星期日。

Wow, the Java date APIs are terrible.

哇,Java 日期 API 太糟糕了。

回答by martin

Using Joda:

使用乔达:

DateTime input;
DateTime startOfLastWeek = input.minusWeeks(1).minusDays(input.getDayOfWeek()-1);

DateTime endOfLastWeek = input.minusWeeks(1).plusDays(input.getDayOfWeek()+1);

DateTime endOfLastWeek = input.minusWeeks(1).plusDays(input.getDayOfWeek()+1);

DateTime endOfLastWeek = startOfLastWeek.plusDays(6);

EDIT:

编辑:

Joda does not allow a different first day of the week, but strictly sticks to the ISO standard, which states that a week always starts on Monday. However, if you need to make that configurable, you could pass the desired first day of the week as a parameter. See the above link for some other ideas.

Joda 不允许一周的第一天不同,但严格遵守 ISO 标准,该标准规定一周总是从 Monday 开始。但是,如果您需要使其可配置,您可以将所需的一周中的第一天作为参数传递。有关其他一些想法,请参阅上面的链接。

public DateTime getFirstDayOfPreviousWeek(DateTime input)
{
    return getFirstDayOfPreviousWeek(input, DateTimeConstants.MONDAY); 
}

public DateTime getFirstDayOfPreviousWeek(DateTime input, int firstDayOfWeek)
{
    return new DateTime(input.minusWeeks(1).withDayOfWeek(firstDayOfWeek));
}

public DateTime getLastDayOfPreviousWeek(DateTime input)
{
    return getLastDayOfPreviousWeek(input, DateTimeConstants.MONDAY); 
}

public DateTime getLastDayOfPreviousWeek(DateTime input, int firstDayOfWeek)
{
    return new DateTime(getFirstDayOfPreviousWeek(input, firstDayOfWeek).plusDays(6));
}

回答by kingori

Calendar today  = Calendar.getInstance();
Calendar lastWeekSunday =  (today.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) ? today.roll(-7): today.roll(Calendar.DAY_OF_YEAR, Calendar.SUNDAY - today.get(Calendar.DAY_OF_WEEK));
Calendar lastWeekMonday = lastWeekSunday.roll( Calendar.DAY_OF_YEAR, -6 );