android - Collapse various date computations into one function -
i have many functions give number of week of current year, week of year give difference of number of year in year minus current week of year , give modulo of difference two.
i want create single method take 2 input, current year("2013") , current date "26/08/2013" , return difference modulo 2 0 or 1.
int totalweeks = gettotalweeksinyear(2013); int currentweeks = getcurrentweekofyear("26/08/2013"); private int gettotalweeksinyear(int year) { calendar cal = calendar.getinstance(); cal.set(calendar.year, year); cal.set(calendar.month, calendar.december); cal.set(calendar.day_of_month, 31); int ordinalday = cal.get(calendar.day_of_year); int weekday = cal.get(calendar.day_of_week) - 1; // sunday = 0 int numberofweeks = (ordinalday - weekday + 10) / 7; system.out.println(numberofweeks); return numberofweeks ; } private int getcurrentweekofyear(string week) { // string dtstart = "26/10/2013"; // input date user string dtstart = week; simpledateformat format = new simpledateformat("dd/m/yyyy"); try { date = format.parse(dtstart); } catch (parseexception e) { // todo auto-generated catch block e.printstacktrace(); } calendar calender = new gregoriancalendar(); calender.settime(date); return calender.get(calendar.week_of_year) ; } int diffrence = totalweeks - currentweeks; int remainder = diffrence % 2; if (remainder == 0) { toast.maketext(this, "current year weeks 0", toast.length_short).show(); } else { if (remainder == 1) { toast.maketext(this, "current year weeks 1" , toast.length_short).show(); } }
int result(int year, string week) { simpledateformat format = new simpledateformat("dd/m/yyyy"); calendar cal1 = calendar.getinstance(); cal1.set(calendar.year, year); cal1.set(calendar.month, calendar.december); cal1.set(calendar.day_of_month, 31); try { date date = format.parse(week); } catch (parseexception e) { e.printstacktrace(); } calendar cal2 = new gregoriancalendar(); calender.settime(date); return ((( cal.get(calendar.day_of_year) - (cal.get(calendar.day_of_week) - 1) + 10) / 7) - cal2.get(calendar.week_of_year) %2) ; }
though wonder, there isn't logic involved why couldn't write it.
Comments
Post a Comment