android - Java adding week to date -
this question has answer here:
- modify week in calendar 4 answers
my code should add 1 week each time runs when setting jaar 2015 , week 50 , running following code multiple times:
gregoriancalendar cal = new gregoriancalendar(); cal.set(calendar.year, jaar); cal.set(calendar.week_of_year, week); cal.add(calendar.week_of_year, 1); jaar = cal.get(calendar.year); week = cal.get(calendar.week_of_year); log.d("jaar", integer.tostring(jaar)); log.d("week", integer.tostring(week));
the output is:
jaar: 2015 week: 51 jaar: 2015 week: 52 jaar: 2016 week: 53 jaar: 2017 week: 2 jaar: 2017 week: 3
so code not working. please help.
edit:
the codes run when user presses icon in tool bar:
public static int jaar; public static int week; public gregoriancalendar cal = new gregoriancalendar(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); gregoriancalendar calendar2 = new gregoriancalendar(); bundle extras = getintent().getextras(); if(extras == null) { week = 50; jaar = calendar2.get(calendar.year); } else { week = extras.getint("week"); jaar = extras.getint("jaar"); } } if (id == r.id.action_volgende) { cal.set(calendar.year, jaar); cal.set(calendar.week_of_year, week); cal.add(calendar.week_of_year, 1); jaar = cal.get(calendar.year); week = cal.get(calendar.week_of_year); log.d("jaar", integer.tostring(jaar)); log.d("week", integer.tostring(week)); intent i=new intent(mainactivity.this,mainactivity.class); i.putextra("week", week); i.putextra("jaar", jaar); startactivity(i); finish();
check values of week , year each time code executes. ran following code similar yours, not exactly, , works expect:
int jaar = 2015; int week = 50; gregoriancalendar cal = new gregoriancalendar(); cal.set(calendar.year, jaar); cal.set(calendar.week_of_year, week); while(true) //loop adds 1 week. { cal.add(calendar.week_of_year, 1); jaar = cal.get(calendar.year); week = cal.get(calendar.week_of_year); system.out.println("jaar: " + jaar); system.out.println("week: " + week); }
results in:
jaar: 2015 week: 51
jaar: 2015 week: 52
jaar: 2016 week: 1
jaar: 2016 week: 2
Comments
Post a Comment