Suppose I have a week that begins with Sunday and ends with Saturday (so UMTWThFS). How do I get the date for each day of the week if, lets say, we use this month and year? I looked in the API but I wasn't sure what to extract to make it work.

10 Years
Discussion Span
Last Post by masijade
Calendar a = new Calendar();
a.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
Date b = Calendar.getTime();

As an example. There's a bit more to it, depending on exactly what you want to do, but that's a start (and more than a start).


Thanks, it worked perfectly.

I have another question though: Given some date, how do I find out what the date for the first day of the week is? For instance,

GregorianCalendar cal = new GregorianCalendar(thisYear, thisMonth, todaysDate);
cal.set(GregorianCalendar.DAY_OF_WEEK, i);
int tempDate = cal.get(GregorianCalendar.DAY_OF_MONTH);

When I do this, the first day of the week is set to whatever todaysDate is, which isn't what I want.


Look at the add method of Calendar (hint, you can give it negative numbers). Cycle backwards and use get(Calendar.DAY_OF_WEEK) and compare it to the GregorianCalendar method getFirstDayOfWeek().


All right I managed to get it. Thanks a bunch.

I'm still a little unclear about some of the fields in Calendar/GregorianCalendar though. I had to find out the hard day that decrementing DAY_OF_MONTH will also affect DAY_OF_WEEK. Am I reading the API wrong? b/c I was under the impression that they were distict fields with DAY_OF_MONTH giving you the date and DAY_OF_WEEK giving you UMTWThFS.


No. If you go backwards a day, you go backwards a day. It's a Calendar. Just like the one you have on the wall.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.