java - Add days, android studio -
in application user can chose date calendar , chose how many days wants add.
adding days working excellent, need skip weekdays.
for example choose day , add +15 days, result saturday or sunday in case need monday if result 1 of these days.
here method adding days
public static date adddays(date date, int days) { calendar cal = calendar.getinstance(); cal.settime(date); cal.add(calendar.date, days + firstclass.hotovo); return cal.gettime(); }
thank help, not expert in programming. amateur , still learning..
you should check resultant day i.e. check if saturday
or sunday
, adding 2 or 1 next monday
.
note: not know firstclass.hotovo have removed temporary below code , can add have in project. below demonstrate how check day , add 1 or 2 day respectively.
here sample code.
caller:
adddays(new date(), 18);
your method:
public static date adddays(date date, int days) { calendar cal = calendar.getinstance(); cal.settime(date); cal.add(calendar.date, days); log.d("test", "before checking: " + cal.gettime().tostring()); // saturday last day of week add 2 days if (cal.get(calendar.day_of_week) == calendar.saturday) { cal.add(calendar.date, 2); // sunday first day of week add 1 day } else if (cal.get(calendar.day_of_week) == calendar.sunday) { cal.add(calendar.date, 1); } // else not required means 1 of week day log.d("test", "after updating: " + cal.gettime().tostring()); return cal.gettime(); }
sample run
resultant day saturday
adding 2 days monday
07-25 15:46:55.729 4219-4219/? d/test: before checking: sat aug 12 15:46:55 pdt 2017 07-25 15:46:55.729 4219-4219/? d/test: after updating: mon aug 14 15:46:55 pdt 2017
resultant day sunday
adding 1 day monday
07-25 15:47:57.634 4322-4322/? d/test: before checking: sun aug 13 15:47:57 pdt 2017 07-25 15:47:57.634 4322-4322/? d/test: after updating: mon aug 14 15:47:57 pdt 2017
resultant day tuesday
not adding more days
07-25 15:52:27.115 4445-4445/? d/test: before checking: tue aug 15 15:52:27 pdt 2017 07-25 15:52:27.115 4445-4445/? d/test: after updating: tue aug 15 15:52:27 pdt 2017
Comments
Post a Comment