15

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

4
  • 1
    @Baadshah, greatly helpful :) +1 Jun 20, 2013 at 10:06
  • @Baadshah Thanks for the help. I was hoping this, Just because I havent included what i have tried so far in some fancy Coding braces. Ofcourse. Jun 20, 2013 at 10:12
  • @RyanMalhotra But if you post what you have tried so far,It would be easy to find whats the mistake,rather than writing all the code our self. Jun 20, 2013 at 10:14
  • 1
    @Ryan Malhotra There are nothing called fancy Coding braces , formatting the code in coding markups really helps. Secondly , if you post something of what you have done then probably make your question look more genuine than a homework !
    – AllTooSir
    Jun 20, 2013 at 10:20

5 Answers 5

36

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

2
  • FYI, the troublesome Calendar class is now legacy, supplanted by the java.time classes. Aug 16, 2017 at 23:53
  • For date 31 DEC 2109 it is returning 2018-12-23 2018-12-29 which is wrong.
    – rajesh
    Dec 31, 2019 at 16:27
22

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

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);
1
  • Thanks.. but Is it not possible in the Calender class ? Jun 20, 2013 at 10:13
3

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

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

}

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Feb 25, 2022 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.