This is driving me nuts!
I have searched everywhere and found for example this question: How to get dates of a week (I know week number)?
However I can't get it to work in my implementation (On ANDROID API19 on NEXUS 7) :
public Pair<String,String> getWeekRange(int year, int week_no) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.WEEK_OF_YEAR, week_no);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
Date monday = cal.getTime();
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
Date sunday = cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
return new Pair<String,String>(sdf.format(monday), sdf.format(sunday));
}
For ANY call to getWeekRange(2014, WEEK)
it ALWAYS gives me the date range of week 2 (which happens to be the current week number today) ??
Calendar seems to be a very confusing class, and there must be something I haven't understood. However according to the docs, YEAR
, WEEK_OF_YEAR
and DAY_OF_WEEK
should be enough for me to set.
Some test data:
01-10 18:28:54.730: V/JSON(24503): Week 1/2014 is date range:2014-01-06 to 2014-01-12
01-10 18:28:54.730: V/JSON(24503): Week 2/2014 is date range:2014-01-06 to 2014-01-12
01-10 18:28:54.730: V/JSON(24503): Week 3/2014 is date range:2014-01-06 to 2014-01-12
01-10 18:28:54.730: V/JSON(24503): Week 4/2014 is date range:2014-01-06 to 2014-01-12
01-10 18:28:54.740: V/JSON(24503): Week 5/2014 is date range:2014-01-06 to 2014-01-12
01-10 18:28:54.740: V/JSON(24503): Week 1/2013 is date range:2013-01-07 to 2013-01-13
01-10 18:28:54.740: V/JSON(24503): Week 2/2013 is date range:2013-01-07 to 2013-01-13
01-10 18:28:54.740: V/JSON(24503): Week 3/2013 is date range:2013-01-07 to 2013-01-13
01-10 18:28:54.740: V/JSON(24503): Week 4/2013 is date range:2013-01-07 to 2013-01-13
01-10 18:28:54.740: V/JSON(24503): Week 5/2013 is date range:2013-01-07 to 2013-01-13
Can anyone see my mistake? Any help is appreciated.
** EDIT **
Seems that the code works fine on Windows, this is an isolated Android issue.