0

Basically I am trying to create a list of all the dates for a week from a set date and store it in a String[] array. But I am having some trouble.

So basically, today is 09/03/2016 so in the String[] array I want to store:

09/03/2016
10/03/2016
11/03/2016
12/03/2016
13/03/2016
14/03/2016
15/03/2016

This is my code:

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() - calendar.get(Calendar.DAY_OF_WEEK));
        String[] weekly = new String[7];
        Arrays.fill(weekly, "");
        int today = calendar.getInstance().get(Calendar.DAY_OF_WEEK);
        for(int i=today; i <= today-1; i++){
            weekly[i] = Integer.toString(i);
            System.out.println(i);
        }

Would be great if someone could help me out

  • 1
    Why are trying store it in first place? Is this somehow relevant in your program? Additionally I advice using JodaTime library or in Java 8 java.time. This will let you easily put all the necessary dates in array. Simple changing it to what you want was explained here http://stackoverflow.com/a/20331243/4130059 – FilMiOs Mar 09 '16 at 23:58
  • Make up your mind. You say you want *"all the dates for a week from a set date"*, and your example of Wednesday March 9 to Wednesday March 16 supports that, but your code seems to try to find the first-day-of-week (Monday or Sunday). Why do that when you don't seem to want that? --- Also, your example has 8 dates listed, but your array is only 7 long. Make up your mind. – Andreas Mar 10 '16 at 00:08
  • @LukazsPioetrszci Further duplicates: [this](http://stackoverflow.com/q/6617854/642706) and [this](http://stackoverflow.com/q/10451784/642706). Please search Stack Overflow before posting. – Basil Bourque Mar 10 '16 at 03:04

1 Answers1

1

I do not see how this for loop is going to work

for(int i=today; i <= today-1; i++){

If you assign the value today to i then it is going to be already greater than today - 1

Also you are not even using the calendar in your loop

How about

for (int i = 0; i < 7; i++) {
    Date dt = calendar.getTime ();
    // now format it using SimpleDateFormat
    String val = df.format (dt);
    weekly[i] = val;
    calendar.add (Calendar.DAY_OF_WEEK, 1);
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • The first line `Date dt = calendar.add (Calendar.DAY_OF_WEEK, 1).getTime ()` does not work. You can't do `getTime()` after, was this even working for you? – Lukazs Pioetrszci Mar 10 '16 at 00:09
  • Yeah, you are right it was a mistake. `was this even working for you?` - sorry I am not going to unit test it for you. – Scary Wombat Mar 10 '16 at 00:12
  • OK, so I tried this and the first date does not start today, it starts on the `07/03/2016` – Lukazs Pioetrszci Mar 10 '16 at 00:16
  • That is because in your code yo do `calendar.add(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() - calendar.get(Calendar.DAY_OF_WEEK))` - WHY? – Scary Wombat Mar 10 '16 at 00:18
  • I can't remember, anyway how do I fix it? – Lukazs Pioetrszci Mar 10 '16 at 00:20
  • Well `Calendar calendar = Calendar.getInstance();` will initialise it to today if that is what you want. – Scary Wombat Mar 10 '16 at 00:22
  • If you want to be a programmer you are going to have to do learn to look for information yourself e.g. javaDocs, google etc – Scary Wombat Mar 10 '16 at 00:23
  • 1
    Ok mate, don't tell me what I need to do if I wish to become a programmer - besides did I ever mention that I wanted to become a programmer? A lot of people have helped me on this forum and have been very supportive even when I have made careless mistakes and serious bugs. Not once have I been told what I needed to do if I wanted to become a programmer. I don't know what you are going to gain from that comment and unlike a lot of people I am fair. You know what, I have accepted your answer as it has helped me – Lukazs Pioetrszci Mar 10 '16 at 00:33