Reputation: 2257
Given a week_of_year, how can I get the dates for the start and end of the week?
Example: Let's say the date is Jan/1/2017.
Calendar calendar = Calendar.getInstance();
// Let's assume that we've set calendar to Jan/1/2017.
Integer week_of_year = calendar.get(Calendar.WEEK_OF_YEAR)
week_of_year would return 1. Presumably, week 1 is anything between Jan/1/2017 to Jan/7/2017.
How can I reverse lookup week_of_year=1 and get the min/max of Jan/7/2017 to Jan/6/2017? or for any other valid week_of_year value.
Upvotes: 0
Views: 68
Reputation: 2795
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.WEEK_OF_YEAR, 1);
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
System.out.println("Start Date: " + sdf.format(cal.getTime()));
cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
System.out.println("End Date: " + sdf.format(cal.getTime()));
Upvotes: 1