15

假设我有一个日期 2013 年 6 月 20 日

如何获得上周的日期范围,即在本例中为 6 月 9 日至 6 月 15 日。

如果日期是 2013 年 6 月 2 日

范围应为 5 月 26 日至 6 月 1 日

4

5 回答 5

35

这是基于 Java 日历的解决方案

    Date date = new Date();
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    int i = c.get(Calendar.DAY_OF_WEEK) - c.getFirstDayOfWeek();
    c.add(Calendar.DATE, -i - 7);
    Date start = c.getTime();
    c.add(Calendar.DATE, 6);
    Date end = c.getTime();
    System.out.println(start + " - " + end);

输出

Mon Jun 10 13:22:01 EEST 2013 - Sun Jun 16 13:22:01 EEST 2013

它是本地化的,在我的语言环境周从星期一开始

于 2013-06-20T10:23:34.870 回答
20

Java 8/11 版本

final ZonedDateTime input = ZonedDateTime.now();
System.out.println(input);
final ZonedDateTime startOfLastWeek = input.minusWeeks(1).with(DayOfWeek.MONDAY);
System.out.println(startOfLastWeek);
final ZonedDateTime endOfLastWeek = startOfLastWeek.plusDays(6);
System.out.println(endOfLastWeek);
于 2016-01-04T15:18:57.583 回答
5

您可以使用JodaTime更清洁的解决方案。你JodaTime可以这样做:

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);
于 2013-06-20T10:06:49.383 回答
3

尝试这个

public static void main(String[] args)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        System.out.println("First Day : " + SampleDateLimit.firstDayOfLastWeek(calendar).getTime());
        System.out.println("Last Day : " + SampleDateLimit.lastDayOfLastWeek(calendar).getTime());
    }

    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;
    }
于 2013-06-20T10:38:02.837 回答
0

使用 JodaTIME 和 Java 8/11

public class DateUtils {
private final DateTime date;

public DateUtils(Date date) {
    this.date = new DateTime(date);
}

public DateMidnight initFirstDayOfLastYear(){
    return new DateMidnight(date.minusYears(1).dayOfYear().withMinimumValue());
}
public Date getFirstDayOfLastYear(){
    return initFirstDayOfLastYear().toDate();
}

public DateMidnight initLastDayOfLastYear(){
    return initFirstDayOfLastYear().dayOfYear().withMaximumValue();
}
public Date getLastDayOfLastYear(){
    return initLastDayOfLastYear().toDate();
}

public DateMidnight initFirstDayOfLastMonth(){
    return new DateMidnight(date.minusMonths(1).dayOfMonth().withMinimumValue());
}

public Date getFirstDayOfLastMonth(){
    return initFirstDayOfLastMonth().toDate();
}

public DateMidnight initLastDayOfLastMonth(){
    return new DateMidnight(initFirstDayOfLastMonth().dayOfMonth().withMaximumValue());
}

public Date getLastDayOfLastMonth(){
    return initLastDayOfLastMonth().toDate();
}

public DateMidnight initFirstDayOfLastWeek(){
    return new DateMidnight(date.minusWeeks(1).dayOfWeek().withMinimumValue());
}
public Date getFirstDayOfLastWeek(){
    return initFirstDayOfLastWeek().toDate();
}

public DateMidnight initLastDayOfLastWeek(){
    return initFirstDayOfLastWeek().dayOfWeek().withMaximumValue();
}
public Date getLastDayOfLastWeek(){
    return initLastDayOfLastWeek().toDate();
}

public DateMidnight initYesterday(){
    return new DateMidnight(date.minusDays(1));
}
public Date getYesterday(){
    return initYesterday().toDate();
}

// this year

public DateMidnight initFirstDayOfYear(){
    return new DateMidnight(date.dayOfYear().withMinimumValue());
}
public Date getFirstDayOfYear(){
    return initFirstDayOfLastYear().toDate();
}

public DateMidnight initLastDayOfYear(){
    return initFirstDayOfLastYear().dayOfYear().withMaximumValue();
}
public Date getLastDayOfYear(){
    return initLastDayOfLastYear().toDate();
}

public DateMidnight initFirstDayOfMonth(){
    return new DateMidnight(date.dayOfMonth().withMinimumValue());
}

public Date getFirstDayOfMonth(){
    return initFirstDayOfLastMonth().toDate();
}

public DateMidnight initLastDayOfMonth(){
    return new DateMidnight(initFirstDayOfMonth().dayOfMonth().withMaximumValue());
}

public Date getLastDayOfMonth(){
    return initLastDayOfLastMonth().toDate();
}

public DateMidnight initFirstDayOfWeek(){
    return new DateMidnight(date.dayOfWeek().withMinimumValue());
}
public Date getFirstDayOfWeek(){
    return initFirstDayOfLastWeek().toDate();
}

public DateMidnight initLastDayOfWeek(){
    return initFirstDayOfWeek().dayOfWeek().withMaximumValue();
}
public Date getLastDayOfWeek(){
    return initLastDayOfWeek().toDate();
}

public DateMidnight initToday(){
    return new DateMidnight(date.minusDays(1));
}
public Date getToday(){
    return initToday().toDate();
}

}

于 2022-02-25T07:49:02.747 回答