Reputation: 1337
I'm writing a program in Java and I need to determine whether a certain date is weekend or not. However, I need to take into account that in various countries weekends fall on different days, e.g. in Israel it's Friday and Saturday wheras in some islamic countries it's Thursday and Friday. For more details you can check out this Wikipedia article. Is there a simple way to do it?
Upvotes: 5
Views: 3306
Reputation: 158
Based on the wiki you've sent I've solved it for myself with the following code:
private static final List<String> sunWeekendDaysCountries = Arrays.asList(new String[]{"GQ", "IN", "TH", "UG"});
private static final List<String> fryWeekendDaysCountries = Arrays.asList(new String[]{"DJ", "IR"});
private static final List<String> frySunWeekendDaysCountries = Arrays.asList(new String[]{"BN"});
private static final List<String> thuFryWeekendDaysCountries = Arrays.asList(new String[]{"AF"});
private static final List<String> frySatWeekendDaysCountries = Arrays.asList(new String[]{"AE", "DZ", "BH", "BD", "EG", "IQ", "IL", "JO", "KW", "LY", "MV", "MR", "OM", "PS", "QA", "SA", "SD", "SY", "YE"});
public static int[] getWeekendDays(Locale locale) {
if (thuFryWeekendDaysCountries.contains(locale.getCountry())) {
return new int[]{Calendar.THURSDAY, Calendar.FRIDAY};
}
else if (frySunWeekendDaysCountries.contains(locale.getCountry())) {
return new int[]{Calendar.FRIDAY, Calendar.SUNDAY};
}
else if (fryWeekendDaysCountries.contains(locale.getCountry())) {
return new int[]{Calendar.FRIDAY};
}
else if (sunWeekendDaysCountries.contains(locale.getCountry())) {
return new int[]{Calendar.SUNDAY};
}
else if (frySatWeekendDaysCountries.contains(locale.getCountry())) {
return new int[]{Calendar.FRIDAY, Calendar.SATURDAY};
}
else {
return new int[]{Calendar.SATURDAY, Calendar.SUNDAY};
}
}
Upvotes: 4
Reputation: 8383
The library, Jollyday could be useful to calculate holidays , http://jollyday.sourceforge.net/index.html
Upvotes: -1
Reputation: 5554
The Java Calendar
class has this functionality, the getFirstDayOfWeek
method. Quote from the Calendar
documentation:
Calendar defines a locale-specific seven day week using two parameters: the first day of the week and the minimal days in first week (from 1 to 7). These numbers are taken from the locale resource data when a Calendar is constructed. They may also be specified explicitly through the methods for setting their values.
So with this information, you are be able to calculate if a day is a weekend day or not.
final Calendar cal = Calendar.getInstance(new Locale("he_IL"));
System.out.println("Sunday is the first day of the week in he_IL? " + (Calendar.SUNDAY == cal.getFirstDayOfWeek()));
Output:
Sunday is the first day of the week in he_IL? true
Upvotes: -1
Reputation: 885
You can get the day of week (see: How to determine day of week by passing specific date?)
Then just check if that day is a weekend based on whatever country is selected.
Upvotes: -1