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

最佳答案

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

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};
    }
}

关于java - 考虑到Java中的当前语言环境,如何检查某个日期是否是周末?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17927854/

10-10 11:58