0

I was having some problem when trying to get the list of dates from previous week based on the specified date. Here is my code:

        SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd");
        Date refDate = null;
        try {
            refDate = formatter.parse("Sun Aug 05");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date[] days = reservationViewModel.getDaysOfWeek(refDate, Calendar.getInstance().getFirstDayOfWeek());
        for (Date day : days) {
            // display            
        }

And the code to get list of dates:

public static Date[] getDaysOfWeek(Date refDate, int firstDayOfWeek) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(refDate);
    calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
    Date[] daysOfWeek = new Date[7];
    for (int i = 0; i < 7; i++) {
        daysOfWeek[i] = calendar.getTime();
        calendar.add(Calendar.DAY_OF_MONTH, 1);
    }
    return daysOfWeek;
}

However, the result that I am getting are:

Sun Aug 02
Mon Aug 03
Tue Aug 04
Wed Aug 05
Thu Aug 06
Fri Aug 07
Sat Aug 08

The dates are all wrong. It supposed to be from 29th July to 4th August. Any ideas? Thanks!

UserError404
  • 117
  • 1
  • 8
  • Search Stack Overflow before posting. This has been covered many times in many ways. – Basil Bourque Aug 06 '18 at 05:51
  • You are using terrible old classes that were supplanted years ago by the *java.time* classes. This has been explained many many times already on Stack Overflow. – Basil Bourque Aug 06 '18 at 05:53
  • Dude, "Sunday", "August", "the 5th". Nowhere does it say of what year. Why do you assume that a computer will be able to guess? Add the year to your String and to your format. Remember to substract a week as the answers tell. Problem solved. – kumesana Aug 06 '18 at 06:16

1 Answers1

1

Move time to 1 week before from given date using Calendar.WEEK_OF_YEAR. Code would be like calendar.set(Calendar.WEEK_OF_YEAR, calendar.get(Calendar.WEEK_OF_YEAR) - 1);

So in your case it would be

public static Date[] getDaysOfWeek(Date refDate, int firstDayOfWeek) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(refDate);
    calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
    calendar.set(Calendar.WEEK_OF_YEAR, calendar.get(Calendar.WEEK_OF_YEAR) - 1);
    Date[] daysOfWeek = new Date[7];
    for (int i = 0; i < 7; i++) {
        daysOfWeek[i] = calendar.getTime();
        calendar.add(Calendar.DAY_OF_MONTH, 1);
    }
    return daysOfWeek;
}

Get previous 7 days

public static Date[] getPrevious7Days(Date refDate) {
    Calendar calendar = Calendar.getInstance();
    int currentYear = calendar.get(Calendar.YEAR);
    calendar.setTime(refDate);
    calendar.set(Calendar.YEAR, currentYear);
    calendar.add(Calendar.DAY_OF_MONTH, -7);

    Date[] daysOfWeek = new Date[7];
    for (int i = 0; i < 7; i++) {
        daysOfWeek[i] = calendar.getTime();
        calendar.add(Calendar.DAY_OF_MONTH, 1);
    }
    return daysOfWeek;
}

It will print like

Sun Jul 29 00:00:00 IST 2018
Mon Jul 30 00:00:00 IST 2018
Tue Jul 31 00:00:00 IST 2018
Wed Aug 01 00:00:00 IST 2018
Thu Aug 02 00:00:00 IST 2018
Fri Aug 03 00:00:00 IST 2018
Sat Aug 04 00:00:00 IST 2018
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • No no what I am trying to do is to get previous week dates based on a specified date. For instance, I get one of the last week date, then I wanted to get the dates two weeks before current date instead of keep getting past week dates based on current date – UserError404 Aug 06 '18 at 05:44
  • @guest176969 that is what I am trying to say here. Just use this method. – Pankaj Kumar Aug 06 '18 at 05:45
  • 1
    @guest176969 Here 1 week before does not mean that you are moving it to 1 week before from current date. It will move 1 week before to the given date. – Pankaj Kumar Aug 06 '18 at 05:46
  • But when I passed in "Sun Aug 05", I am getting "Sun Jul 26" all the way to "Sat Aug 01" which is wrong. Do you have any ideas why is it so? it supposed to be Jul 29 to Aug 04 – UserError404 Aug 06 '18 at 05:51
  • @guest176969 API is doing it correctly but you are using it in a wrong way. What is your expectation? Are you trying to get 7 days which is just before given date? or you are trying to get 1 week before dates? both are completely different things. – Pankaj Kumar Aug 06 '18 at 05:55
  • You are using a date-time type (`Calendar`) where a date-only type (`LocalDate`) is needed. – Basil Bourque Aug 06 '18 at 05:55
  • Because I will always pass in Sunday, so I need to get the dates of last Sunday to last Saturday. – UserError404 Aug 06 '18 at 05:57
  • @BasilBourque That is correct. But that API is added into java8 and I am not sure what version he is using. So I just answered the correct way into what he is using. – Pankaj Kumar Aug 06 '18 at 05:58
  • @PankajKumar Hey do you have any ideas? Because the dates and the days of week seems wrong. I printed out the dates, it becomes 07/26/1970 .. – UserError404 Aug 06 '18 at 06:04
  • @guest176969 First you need to explain your problem. I am still not sure what you want. And if you are sure about inputed date which is always be sunday then what was the reason of calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek); – Pankaj Kumar Aug 06 '18 at 06:12
  • Okay what I am trying to do is get the list of dates for previous weeks. I will always pass in Sunday, which is the start of the week into the method above. Then I wanted to get the list of dates for previous week starting from Sunday and ending on Saturday. But when I print out the dates returned from method above, I am getting the dates in 1970 which I have no idea why is it so – UserError404 Aug 06 '18 at 06:15
  • @guest176969 Check updated answer. And the reson of 1970 is, you are parsing date with month and day only not with year, so it is taking default year that 1970. If you want to take year also, you need to use EEE MMM dd formatter and pass value for year also – Pankaj Kumar Aug 06 '18 at 06:18
  • I see I see thanks so much! – UserError404 Aug 06 '18 at 06:27