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?
-
It doesn't seem to me like that information is stored in a Locale. – Sotirios Delimanolis Jul 29 '13 at 15:21
-
You should really consider switching to JodaTime instead of using Java's date/time stuff. (Unless you're using Java 8, and don't need to support prior Java frameworks - JodaTime is more-or-less built in to Java 8, under a different name.) – RenniePet Sep 27 '14 at 16:30
-
@RenniePet Yes [Joda-Time](http://www.joda.org/joda-time/) is a wonderful library. But I don‘t see how it helps with this question. As far as I know, Joda-Time does not include any locale-type of information regarding weekdays versus weekend. Joda-Time does support [ISO 8601](http://en.wikipedia.org/wiki/ISO_8601) standard weeks, but not other culture-based weeks. Please post an Answer if you have some insight or example code you can share. – Basil Bourque Sep 27 '14 at 23:46
-
possible duplicate of [java example to get all weekend dates in a given month](http://stackoverflow.com/questions/3272454/java-example-to-get-all-weekend-dates-in-a-given-month) – Basil Bourque Sep 27 '14 at 23:50
-
@BasilBourque: OK, sorry, I stand corrected. I'm fairly new to Java, and have became aware of various problems with the built-in Java date/time and calendar facilities, and the standard advice is always "switch to JodaTime" (which I have done in my little app), so I had just assumed it would also help here. Thanks for informing me. – RenniePet Sep 28 '14 at 00:19
-
@RenniePet No need to apologize. We are here to learn and explore. I was asking, not criticizing. Joda-Time is a huge project with many nooks and crannies. And now the java.time project adds another dimension. If there some solution to this problem of weekends and holidays, I'd want to hear it. So far it seems the only two solutions are [Jollyday](http://jollyday.sourceforge.net/index.html) and roll-your-own. – Basil Bourque Sep 28 '14 at 03:15
4 Answers
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};
}
}

- 148
- 9
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.
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

- 5,556
- 2
- 29
- 48
-
6it's Sunday in en_US according to that. The big flaw here: `getFirstDayOfWeek` returns the day that is commonly the first on printed calendars, not the first day of the working week. http://ideone.com/zX4Pcc – zapl Jul 29 '13 at 15:50
The library, Jollyday could be useful to calculate holidays , http://jollyday.sourceforge.net/index.html

- 8,265
- 6
- 25
- 36