Calendar question

Reply

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Calendar question

 
0
  #1
May 8th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,356
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Calendar question

 
0
  #2
May 8th, 2008
  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).
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: Calendar question

 
0
  #3
May 8th, 2008
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,

  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,356
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Calendar question

 
0
  #4
May 8th, 2008
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().
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: Calendar question

 
0
  #5
May 8th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,356
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Calendar question

 
0
  #6
May 8th, 2008
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC