File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how to caculate first day / last day of a week ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to caculate first day / last day of a week ?" Watch "how to caculate first day / last day of a week ?" New topic
Author

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

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
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.
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
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.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Jodatime is easier.
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
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

Joined: Dec 23, 2003
Posts: 798
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

Joined: Feb 10, 2009
Posts: 710
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.
Edgar Balderas
Greenhorn

Joined: Jun 13, 2009
Posts: 14
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).


|SCJP-87|SCWCD-95|
Edgar Balderas
Greenhorn

Joined: Jun 13, 2009
Posts: 14
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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

Joined: Jun 13, 2009
Posts: 14
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to caculate first day / last day of a week ?
 
Similar Threads
Date
SCDJWS Study group in Toronto
date question
WEEK_OF_MONTH with alternate result
first day of the current week