简体   繁体   中英

Get last week date range for a date in Java

Suppose I have a Date 20 June 2013

How can I get the Date range for the last week, ie in this case 9 June to 15 June.

Also if the date was 2nd June 2013

the range should be 26 may to 1 june

this is Java Calendar based solution

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

output

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

it's localized, in my Locale week starts with Monday

Java 8/11 version

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

You can use JodaTime for a cleaner solution. With JodaTime you can do as below:

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

Try this

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

WITH JodaTIME and 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();
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM