Let me explain myself. By knowing the week number and the year of a date:
Date curr = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(curr);
int nweek = cal.WEEK_OF_YEAR;
int year = cal.YEAR;
But now I don't know how to get the date of the first day of that week. I've been looking in Calendar, Date, DateFormat but nothing that may be useful...
Any suggestion? (working in Java)
Try this:
Try the Gregorian Calendar algorithm:
I haven't done much Date stuff in java but a solution might be:
Logic:
Get the day of the week and substract it from the current date (might need -1, depending on wether you need monday to be first day of the week or sunday)
Yet another way...
cal is now the first day of the week
1-cal.get(GregorianCalendar.DAY_OF_WEEK)
evaluates to 1-1 for Sunday (first day of week in my Locale) and 1-2 for Monday, so this will give you the correction needed to rewind the clock back to Sunday
Here's some quick and dirty code to do this. This code creates a calendar object with the date of the current day, calculates the current day of the week, and subtracts the day of the week so you're on the first one (Sunday). Although I'm using DAY_OF_YEAR it goes across years fine (on 1/2/10 it'll return 12/27/09 which is right).
Be cautious with those,
calendar.get(Calendar.WEEK_OF_YEAR)
returns 1 if it is end of December and already a week that ends in the next year.Using
sets the cal2 date to e.g. end of 2009 on 29/12/2010 !!
I used this:
I also make sure that weeks in my calendars, no matter what locale they are in, are starting on Mondays: