svendiedrichsen / jollyday

Jollyday - A holiday API
Other
189 stars 114 forks source link

Is it possible to create a HolidayManager from a given Locale? #38

Closed MoriTanosuke closed 7 years ago

MoriTanosuke commented 7 years ago

I already asked this question on StackOverflow, but already got a comment that the question might be offtopic... I hope this is the right place to ask this.

In my application the user can set her Locale from a list of available locales. The application then needs to determine if a given date is a holiday.

Here is my current code:

public boolean isHoliday(Calendar cal) {
    HolidayManager m = HolidayManager.getInstance();
    return m.isHoliday(cal);
}

I can change this to the following code to set a specific HolidayCalendar:

public boolean isHoliday(Calendar cal) {
    HolidayManager m = HolidayManager.getInstance(HolidayCalendar.GERMANY);
    return m.isHoliday(cal);
}

But this method is deprecated and I would need to figure out which HolidayCalendar I need based on the user locale. What I want to do is something like this:

public boolean isHoliday(Calendar cal, Locale loc) {
    HolidayManager m = HolidayManager.getInstance(loc);
    return m.isHoliday(cal);
}

I know that HolidayManager is using Locale.getDefault() and there are public methods in Holiday too (see here) but I can not figure out how to do this and the documentation is very brief.

My question: Can I get the holidays based on a particular locale? Do I have to write a major switch statement for all the locales to match a HolidayCalendar?

svendiedrichsen commented 7 years ago

Hi, generally the holiday API i working with holiday calendar XML files. There are a couple of examples which basically assemble holidays of a specific country. But this does not have be a country. There is also a holiday calendar named 'nyse' which has a list of holidays for the New York Stock Exchange. You can create your own XML file, put it in your apps classpath and use it with the API. Cheers, Sven