• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

date question

 
bala kiran
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you look at the Calendar, GregorianCalendar?
 
bala kiran
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did. If you have any specific suggestion, thats appreciated.
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.

cheers
[ October 11, 2004: Message edited by: Sonny Gill ]
 
Sreenivasa Majji
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic