How to check if a certain date is a weekend given the current language in Java?

I am writing a program in Java and I need to determine if a certain date will be a weekend or not. However, I need to consider that in different countries, weekends fall on different days, for example. in Israel it is Friday and Saturday, in some Islamic countries it is Thursday and Friday. For more information, you can check out this Wikipedia article . Is there an easy way to do this?

+4
source share
4 answers

Based on the wiki you submitted, I 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}; } } 
+2
source

You can get the day of the week (see How to determine the day of the week by going through a specific date? )

Then just check if this day is a holiday, depending on the country you have selected.

0
source

The Java Calendar class has this functionality, the getFirstDayOfWeek method. Quote from the Calendar documentation:

The calendar defines a seven-day weekly week using two parameters: the first day of the week and the minimum days in the first week (from 1 to 7). These numbers are taken from the locale resource data when constructing the Calendar. They can also be specified explicitly through methods to set their values.

Thus, with this information you can calculate whether the day is a day off 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 
0
source

A library, Jollyday can be useful for calculating holidays, http://jollyday.sourceforge.net/index.html

0
source

Source: https://habr.com/ru/post/1494085/


All Articles