0

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;
}
Rosso
  • 93
  • 2
  • 9
  • 1
    I would add two lists: one for holiday within year (you have it as holidaysList) and second as recurringHolidayList. Inside isHoliday() I would add another or condition isRecurringHoliday(date). isRecurringHoliday() would go through all elements of recurringHolidayList and compare if date.getDay().equals(element.getDay()) and date.getMonth().equals(element.getMonth()). Correct me if I missunderstood something. – t4u Jun 10 '20 at 22:24
  • @t4u That would be a good start and thank you, but this only checks if the recurring holiday exists in a seperated List doesnt it? What i need is further implementation of the logic inside setRecurringHoliday. How do I implement "if it's a new year, set all Holidays stored in recurringHolidayList for this year"? – Rosso Jun 10 '20 at 22:47
  • Nevermind your tip helped! I posted a solution below. thanks a lot. – Rosso Jun 12 '20 at 23:46

1 Answers1

2

You are using terrible date-time classes that were years ago supplanted by the java.time classes defined in JSR 310.

MonthDay

For a month and day-of-month without year, use MonthDay.

MonthDay xmas = MonthDay.of( Month.DECEMBER , 25 ) ;

Your set of holidays should be a set MonthDay objects, from what I can discern of your Question. I find your overall Question confusing as its logic falls short of what you need for the usual workplace holiday tracking.

Set< MonthDay > holidays = new TreeSet<>() ;
holidays.add( xmas ) ;

For a date, use LocalDate.

Apply a year to get a date.

LocalDate xmas2020 = xmas.atYear( 2020 ) ;

To get current year, use Year, and specify a time zone. For any given moment, the date, and therefore possibly the year, varies around the globe by zone.

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
Year currentYear = Year.now( z ) ;
LocalDate xmasThisYear = currentYear.atMonthDay( xmas ) ;

Add to Year for next year.

Year nextYear = currentYear.plusYears( 1 ) ;
LocalDate xmasNextYear = nextYear.atMonthDay( xmas ) ;

Ask if a date is this year or next.

boolean isThisYear = Year.from( localDate ).equals( currentYear ) ;
boolean isNextYear = Year.from( localDate ).equals( nextYear ) ;
boolean isFutureYear = Year.from( localDate ).isAfter( currentYear ) ;

For your check of the weekend, define an EnumSet of the desired day of week values as defined in the DayOfWeek enum.

Set< DayOfWeek > weekend = EnumSet.of( DayOfWeek.SATURDAY , DayOfWeek.SUNDAY ) ;
boolean isWeekend = weekend.contains( localDate.getDayOfWeek() ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • The reason I am using these old classes is because the task given to me describes the solution with these classes, but I am not sure if I HAVE to use them and problem is that I am not allowed to ask them either..And thank you for correcting that. I will see if I can solve this using these steps. – Rosso Jun 11 '20 at 07:49