I am making a calendar that allows you to add a specific holiday which recurs each year automatically. My WorkdayCalendar.class needs 2 methods: -setHoliday(Calendar date) which sets a holiday only within that year -setRecurringHoliday(Calendar date) which should (preferably) use setHoliday() and set it recurring each year. How do I implement the logic that checks if it is a new year? I am adding holidays to a HashSet named holidaysList. I need a method that checks if it is a new year and then adds a specified holiday. The setHoliday works fine and has been tested wih unitTests.
public void setHoliday(Calendar date) {
this.date = date.getTime();
if (!isHoliday(date)) {
holidaysList.add(this.date);
}
}
public void setRecurringHoliday(Calendar date) {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");
GregorianCalendar todaysDate = new GregorianCalendar();
System.out.println(
sdf.format("Todays date: " + todaysDate.getTime()) + "\n");
int thisYear = todaysDate.get(Calendar.YEAR);
int chosenYear = date.get(Calendar.YEAR);
System.out.println("Chosen year: " + chosenYear + "\nThis year: " + thisYear);
date.add(Calendar.YEAR, 1);
int nextYear = date.get(Calendar.YEAR);
System.out.println("Next year: " + nextYear);
/*What to do here???*/
if (thisYear == nextYear){
setHoliday(date);
System.out.println("recurring holiday added");
}
}
private boolean isHoliday(Calendar date) {
this.date = date.getTime();
return isWeekend(date) || holidaysList.contains(this.date);
}
private boolean isWeekend(Calendar date) {
int chosenDay = date.get(Calendar.DAY_OF_WEEK);
return chosenDay == Calendar.SATURDAY || chosenDay == Calendar.SUNDAY;
}