Reputation: 5954
I am trying to get the dates from a given weeknumber and the year. The below piece of code is always showing the currentDates even though I enter week and year.
Calendar c = new GregorianCalendar(Locale.getDefault());
c.set(Calendar.WEEK_OF_YEAR, 15);
c.set(Calendar.YEAR, 2015);
String result = "";
int firstDayOfWeek = c.getFirstDayOfWeek();
for (int i = firstDayOfWeek; i < firstDayOfWeek + 7; i++) {
c.set(Calendar.DAY_OF_WEEK, i);
result += new SimpleDateFormat("yyyy.MM.dd").format(c.getTime()) + "\n";
}
instead of getting dates of Week number 15. I am getting current dates values always. I am not what is wrong here?
How to solve this issue?
Thanks!
Upvotes: 1
Views: 669
Reputation:
Try this
Calendar now = Calendar.getInstance();
now.set(Calendar.YEAR,2015);
now.set(Calendar.WEEK_OF_YEAR,15);
System.out.println(new SimpleDateFormat("yyyy.MM.dd").format(now.getTime()));
OUTPUT
Upvotes: 1
Reputation: 1413
please update the my code: it's works for me.
Calendar now = Calendar.getInstance();
now.set(Calendar.YEAR,2015);
now.set(Calendar.WEEK_OF_YEAR,15);
String result = "";
int firstDayOfWeek = now.getFirstDayOfWeek();
Log.e("day","day:"+firstDayOfWeek);
for (int i = firstDayOfWeek; i < firstDayOfWeek + 7; i++) {
now.set(Calendar.DAY_OF_WEEK, i);
result += new SimpleDateFormat("yyyy.MM.dd").format(now.getTime()) + "\n";
}
Log.e("result","result:"+result);
Upvotes: 0