5

我正在用 Java 编写一个程序,我需要确定某个日期是否是周末。然而,我需要考虑到,在不同的国家,周末在不同的日子,例如在以色列是星期五和星期六,而在一些伊斯兰国家是星期四和星期五。有关更多详细信息,您可以查看此 Wikipedia 文章。有没有简单的方法来做到这一点?

4

4 回答 4

3

根据您发送的 wiki,我已使用以下代码为自己解决了这个问题:

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};
    }
}
于 2014-09-27T16:22:28.297 回答
0

您可以获得星期几(请参阅:如何通过特定日期确定星期几?

然后只需根据选择的任何国家/地区检查那一天是否是周末。

于 2013-07-29T15:21:50.563 回答
0

图书馆,Jollyday可用于计算假期,http://jollyday.sourceforge.net/index.html

于 2013-07-29T15:34:32.107 回答
0

JavaCalendar类有这个功能,getFirstDayOfWeek方法。从Calendar文档中引用:

Calendar 使用两个参数定义特定于语言环境的一周 7 天:一周的第一天和第一周的最少天数(从 1 到 7)。这些数字是在构造日历时从语言环境资源数据中获取的。它们也可以通过设置它们的值的方法明确指定。

因此,有了这些信息,您就可以计算出一天是否是周末。

    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()));

输出:

Sunday is the first day of the week in he_IL? true
于 2013-07-29T15:23:23.660 回答