943,685 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 667
  • Java RSS
May 8th, 2008
0

Calendar question

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
May 8th, 2008
0

Re: Calendar question

Java Syntax (Toggle Plain Text)
  1. Calendar a = new Calendar();
  2. a.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  3. 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).
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
May 8th, 2008
0

Re: Calendar question

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,

Java Syntax (Toggle Plain Text)
  1. GregorianCalendar cal = new GregorianCalendar(thisYear, thisMonth, todaysDate);
  2. cal.set(GregorianCalendar.DAY_OF_WEEK, i);
  3. 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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
May 8th, 2008
0

Re: Calendar question

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().
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
May 8th, 2008
0

Re: Calendar question

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
May 8th, 2008
0

Re: Calendar question

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java using BlueJ- GUI (Graphic User Interface)
Next Thread in Java Forum Timeline: Problem in creating a jar file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC