File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes date question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "date question" Watch "date question" New topic

date question

bala kiran
Ranch Hand

Joined: Jun 11, 2004
Posts: 34
I want to know, given today's date, how to find the date for first day of the current week. For example, today is: Oct-14-04. First day of the current week is Oct-11-04 (assuming monday is the first day of the week). How can I get this? Any help is highly appreciated.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Did you look at the Calendar, GregorianCalendar?
bala kiran
Ranch Hand

Joined: Jun 11, 2004
Posts: 34
I did. If you have any specific suggestion, thats appreciated.
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Now, the calendar is set to the Monday of this week.

Dates in Java can be a bit tricky. I would suggest that you write a lot of unit tests to make sure that your code is working as intended.

[ October 11, 2004: Message edited by: Sonny Gill ]

The future is here. It's just not evenly distributed yet. - William Gibson
Sonny Gill LinkedIn
Sreenivasa Majji
Ranch Hand

Joined: Jul 12, 2001
Posts: 224
Calendar today = Calendar.getInstance();

int year = today.get(Calendar.YEAR);
int month = today.get(Calendar.MONTH);
int day = today.get(Calendar.DATE);

System.out.println(year +"-" +(month +1) +"-" +day);

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

year = today.get(Calendar.YEAR);
month = today.get(Calendar.MONTH);
day = today.get(Calendar.DATE);

System.out.println(year +"-" +(month +1) +"-" +day);

Hope this helps,

Sreenivasa Majji
I agree. Here's the link:
subject: date question
It's not a secret anymore!