• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to caculate first day / last day of a week ?

 
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how could I get first / last day of a week based on a specified date ? Like, today is June 17, 2009, then I need to get June 14, 2009 as first day, June 20 as last day of this week .

Thanks.
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To start, I would look at the documentaiton for the Calendar class and see what it provides. From there you should be able to get a good start.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jodatime is easier.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:Jodatime is easier.



I've never used it, but I'm sure it is since I find the Calendar class somewhat difficult to work with. I usually suggest the Calendar class since I know that if they are using Java they will have access to that, and some people may not be able to install other APIs.

But I would still recommend David's idea too, if it is feasible.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

W. Joe Smith wrote:To start, I would look at the documentaiton for the Calendar class and see what it provides. From there you should be able to get a good start.


The getFirstDayOfWeek() should definitely help you out.
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:
The getFirstDayOfWeek() should definitely help you out.



I have read the API, found it is not what I want.



public int getFirstDayOfWeek()

Gets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France.


it will just tell if the first day is SUNDAY or MONDAY.
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As advised, I am using Calendar, but it doesn't work for me.

I roll back six months, but it will show up "12/17/2009". How to fix it ?

 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edward Chen wrote:As advised, I am using Calendar, but it doesn't work for me.

I roll back six months, but it will show up "12/17/2009". How to fix it ?



I believe that subtracting months doesn't have any effect on years. I think your best bet would be to write a small check to see if the number of months being subtracted (or added) would push into a different year, then adjust the year accordingly.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should get the day of the week with Calendar.get(Calendar.DAY_OF_WEEK) and compare this with SUNDAY/MONDAY (depending which your first day of a week). And update a new Date with the day you input minus the total of milliseconds of a day, and compare this again in a loop until your Calendar.DAY_OF_WEEK equals the day you want (SUNDAY/MONDAY).
 
Edgar Balderas
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made a little snippet that do what you need:





You pass to the methods a Calendar object, like today:
Calendar cal = Calendar.getInstance();
cal.set(2009,5,17);
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

W. Joe Smith wrote:I believe that subtracting months doesn't have any effect on years. I think your best bet would be to write a small check to see if the number of months being subtracted (or added) would push into a different year, then adjust the year accordingly.


The add method does change years etc. It's the roll method that only changes the field you are "rolling". In this case, the months are changed but the rest stays the same.

Edgar Balderas wrote:I made a little snippet that do what you need:




What happens if --day becomes 0, or ++day overflows to the next year (366 / 367)?

Two improvements:
1) use Calendar.getFirstDayOfWeek() instead of hardcoding Calendar.SUNDAY and Calendar.SATURDAY. The following can be used to get the last weekday of the week:
Alternatively:

2) Use cal.add(Calendar.DAY_OF_YEAR, -1) and cal.add(Calendar.DAY_OF_YEAR, 1) instead of setting the day of year.
 
Edgar Balderas
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:What happens if --day becomes 0, or ++day overflows to the next year (366 / 367)?



Yeaah, I thought that later, and made something like this:

Where ONE_DAY is the long static value in millis (86400000). But your code using:

Rob Prime wrote:Use cal.add(Calendar.DAY_OF_YEAR, -1) and cal.add(Calendar.DAY_OF_YEAR, 1) instead of setting the day of year.

instead of using Calendar.setTimeInMillis(long) works great also.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works even better. Your code may eventually fail due to daylight savings time, when a day can be 23 or 25 hours. Calendar.add knows how to handle those cases.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic