How to add holidays in Android Calendar? -


i trying add list of holidays calendar. using caldroid library displaying calendar. want display list of holidays in every month need select specific dates in every month. how do ? following have tried:

code :

calendar cal = calendar.getinstance(); cal.add(calendar.date, -18); date bluedate = cal.gettime(); cal = calendar.getinstance(); cal.add(calendar.date, 16); int diff = cal.get(calendar.january); cal.add(diff, 10); date greendate = cal.gettime();  

i believed diff set month january , highlight 11th of january cause have given value 10 doesn't , believe because have instantiated cal getinstance() return current month.

update :

thanks meno, have achieved following when set calendar second time, takes updates value , not set first date (very obvious) want know how set multiple dates in month without re-instantiating new gregoriancalendar object every month. put, how set array of dates in month.

gregoriancalendar greg_cal = new gregoriancalendar(); greg_cal.set(calendar.month, calendar.january); greg_cal.set(calendar.day_of_month, 1); greg_cal.set(calendar.month, calendar.january); greg_cal.set(calendar.day_of_month, 18); 

thanks in advance.

your question not appear clear. nevertheless try answer. instead of

cal = calendar.getinstance(); // in thailand gives buddhist calendar, want this? cal.add(calendar.date, 16); // 16 days now, intention or meaning??? cal.add(diff, 10); // first argument must defined constant in java.util.calendar 

i assume want select fixed date (as holiday). if can call set()-method , don't need add days move calendar date forth , back:

gregoriancalendar cal = new gregoriancalendar(); // including currrent year cal.set(calendar.month, calendar.january); cal.set(calendar.day_of_month, 11); 

then date 11th of january in current year. way:

int diff = cal.get(calendar.january); 

this line nonsense because:

calendar.january int constant 0 , denotes value (the month) not field. get(int)-method expects field constant. field constant value 0 corresponds calendar.era. line yields era of cal, namely int diff = gregoriancalendar.ad = 1; assuming use gregorian calendar. surely not want???

updated because of question in comment:

reusing means don't create new instance next calculation reuse same 1 (gregoriancalendar mutable!). example:

gregoriancalendar cal = new gregoriancalendar(); // including currrent year cal.set(calendar.month, calendar.january); cal.set(calendar.day_of_month, 11); date holiday1 = cal.gettime();  cal.set(calendar.month, calendar.december); // no new instance => reuse cal cal.set(calendar.day_of_month, 24); date holiday2 = cal.gettime();  ... 

i have written limiting manipulations of month , day-of-month because manipulation of week-related fields state of reused calendar-instance depends on order of field manipulations (very ugly , surprising).

anyway, safer use immutable types available in java 8 (not useable on android), jodatime , alpha-state-library. admit first contact jodatime can cause feeling lost because there many methods (the documentation standard open-source less example in jsr-310). in use-case use type org.joda.time.localdate start because have plain-date-use-case. google , friends if want see more documentation beyond original joda documentation.

update due extended question:

you have forgotten 1 important thing in new code, namely add results of calendar setting holiday list, see here modification:

list<date> holidays = new arraylist<date>();  gregoriancalendar greg_cal = new gregoriancalendar(); greg_cal.set(calendar.month, calendar.january); greg_cal.set(calendar.day_of_month, 1); holidays.add(greg_cal.gettime());  greg_cal.set(calendar.month, calendar.january); greg_cal.set(calendar.day_of_month, 18); holidays.add(greg_cal.gettime());  simpledateformat sdf = new simpledateformat("yyyy-mm-dd");  (date d : holidays) {   system.out.println(sdf.format(d)); }  // output: 2014-01-01 2014-01-18 

in external library jodatime use org.joda.time.localdate instead.

list<localdate> holidays = new arraylist<localdate>(); localdate today = localdate.now(); holidays.add(today.withmonthofyear(1).withdayofmonth(1)); holidays.add(today.withmonthofyear(1).withdayofmonth(18)); 

it pretty simple (similar in unfinished date-and-time-library, too).


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -