java - Iterate on all weeks between start, finish date -
i have 2 date objects, start , finish. want iterate them on weekly basis, i.e. if there 4 weeks in between (calendar weeks, not 7 days after each other), want 4 iterations , in each iteration want actual start , end dates.
i'm hacking iterable purpose, i'm thinking if can achieved example joda time or smart custom method. in advance!
edit: must repeat need weeks in calendar, not 7 days after each other. if start date on random day in week (for example friday), first iteration should contain [friday,sunday] not [friday,friday+7 days]. solution posted answer.
to calculate using calendar , week_of_year:
int startweek; int finishweek; int diff; simpledateformat sdf; calendar cal; calendar startcountingcal; date startdate; date finishdate; string startdates = "01/01/2013"; string finishdates = "01/05/2013"; sdf = new simpledateformat("dd/mm/yyyy"); startdate = sdf.parse(startdates); finishdate = sdf.parse(finishdates); cal = calendar.getinstance(); cal.settime(startdate); startweek = cal.get(calendar.week_of_year); cal.settime(finishdate); finishweek = cal.get(calendar.week_of_year); diff = finishweek - startweek; startcountingcal = calendar.getinstance(); startcountingcal.settime(startdate); (int = 0; < diff; i++) { if (i==0) { system.out.println("week " + + " start: " + sdf.format(startcountingcal.gettime())); startcountingcal.add(calendar.date, 7); system.out.println("week " + + " start: " + sdf.format(startcountingcal.gettime())); } else { system.out.println("week " + + " start: " + sdf.format(startcountingcal.gettime())); startcountingcal.add(calendar.date, 7); system.out.println("week " + + " start: " + sdf.format(startcountingcal.gettime())); } }
output:
week 0 start: 01/01/2013 week 0 start: 08/01/2013 week 1 start: 08/01/2013 week 1 start: 15/01/2013 week 2 start: 15/01/2013 week 2 start: 22/01/2013 week 3 start: 22/01/2013 week 3 start: 29/01/2013 .... etc.
hope helps!
Comments
Post a Comment