Java日历:计算上周的开始日期和结束日期

时间:2011-10-12 15:12:57

标签: java

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

6 个答案:

答案 0 :(得分:6)

这是另一个JodaTime解决方案。既然你似乎只想要Dates(不是时间戳),我会使用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);

<强>输出:

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

答案 1 :(得分:3)

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;
}

答案 2 :(得分:2)

如果不涉及第三方图书馆,我会去@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 };
}

答案 3 :(得分:0)

您可以使用带参数Calendar.DATE的{​​{3}}和当周某一天的偏移量:

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 };
}

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

哇,Java日期API非常糟糕。

答案 4 :(得分:-1)

使用Joda:

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

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

DateTime endOfLastWeek = startOfLastWeek.plusDays(6);

编辑:

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。但是,如果您需要进行可配置,则可以将所需的第一天作为参数传递。有关其他一些想法,请参阅上面的链接。

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));
}

答案 5 :(得分:-1)

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 );