• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Getting the day of Monday given the date of other day in that week

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a instance of Calendar which is set to say
day = 25
month = 7
year = 2006

Now the day comes to be Friday.

I need to extract the date of the immediate previous Monday, which in this case is the 21st.

Is there some way to do that combing given functions and constants of the Calendar class???
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ofcourse there is, and it's very simple.

You need to use the set(...) method in class Calendar. The field you want to set is Calendar.DAY_OF_WEEK and you'll want to set it to Calendar.MONDAY.

[ August 10, 2006: Message edited by: Jesper Young ]
 
vikas Chitte
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i can't change the calendar object.

now a obvious way out is by looping backwards untill the day != Calendar.MONDAY.

What i want to know is that is there any way to avoid that looping using combinations of funtions and the constants.
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just clone the Calendar you have using the clone() method and perform what the previous poster said on the cloned object. That'll give you your Monday and it will keep your original Calendar unchanged.
 
Lanny Gilbert
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's some code I tried that works just fine.

Calendar cal = Calendar.getInstance();

// Set the calendar to August 4th, 2006
cal.set(2006, Calendar.AUGUST, 4);

Calendar clonedCal = (Calendar)cal.clone();


clonedCal.set(Calendar.WEEK_OF_MONTH,
cal.get(Calendar.WEEK_OF_MONTH));

clonedCal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

System.out.println(clonedCal.getTime());
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vikas Chitte:
i can't change the calendar object.



Ah, a homework question. That's the only style of question that would have such a requirement

Just create another Calendar based on the contents of the one you have and voila
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic