首页 文章

最后一个工作日不是假日

提问于
浏览
2

我有以下 Holiday 课程:

public class Holiday {

    private int day;
    private int month;


    public Holiday(GregorianCalendar calendar) {
        this.day = calendar.get(GregorianCalendar.DAY_OF_MONTH);
        this.month = calendar.get(GregorianCalendar.MONTH) + 1;
    }

}

枚举日:

public enum Day {

    SATURDAY(6), SUNDAY(7);

    private int index;

    Day(int index) {
        this.index = index;
    }

    public int getIndex() {
        return index;
    }
}


public class DateTool {

    private static final String DATE_FORMAT = "yyyy_MM_dd";

    public DateTool() {
        super();
    }


    public static String getPreviousWorkingDay(List<Holiday> listOfHolidays) {
            //derive the last working day that is not saturday/sunday
    }

    public static String parseDate(Date date) {
        return new SimpleDateFormat(DATE_FORMAT).format(date);
    }

    public static boolean isSunday(LocalDateTime date) {
        return date.getDayOfWeek().getValue() == Day.SUNDAY.getIndex();
    }

    public static boolean isSaturday(LocalDateTime date) {
        return date.getDayOfWeek().getValue() == Day.SATURDAY.getIndex();
    }

}

鉴于我有 holidays holidays ,我如何计算并以 getPreviousWorkingDay(...) 方法返回上一个上一个工作日,这将排除星期六和星期日?

我试图导出最后一个文件日期来寻找这样的东西,我正在尝试解决

if (todayIsHoliday(listOfHolidays)) {
                getPreviousWorkingDay(listOfHolidays);
            }

所以如果当天是假日,请查看最后一个非假日日期并以字符串格式返回 .

我不确定如何回顾 . 请注意,假期列表不仅仅是周六和周日 . 他们是乡村假期,例如农历新年等 .

我正在使用java 8,所以欢迎任何重构或改进:)

2 回答

  • 4
    private static final DateTimeFormatter dateFormatter
            = DateTimeFormatter.ofPattern("uuuu_MM_dd");
    
    public static String getPreviousWorkingDay(List<MonthDay> listOfHolidays) {
        LocalDate workingDay = LocalDate.now(ZoneId.of("Pacific/Easter")).minusDays(1);
        while (workingDay.getDayOfWeek().equals(DayOfWeek.SATURDAY) 
                || workingDay.getDayOfWeek().equals(DayOfWeek.SUNDAY)
                || listOfHolidays.contains(MonthDay.from(workingDay))) {
            workingDay = workingDay.minusDays(1);
        }
        return workingDay.format(dateFormatter);
    }
    

    我正在使用现代Java日期和时间API java.time ,并且如评论中所述,我建议您也这样做 . 让我们看看上面的方法:

    System.out.println(getPreviousWorkingDay(Collections.emptyList()));
        // Let’s say Valentin’s day is a holiday
        System.out.println(getPreviousWorkingDay(List.of(MonthDay.of(Month.FEBRUARY, 14))));
        // And so are Lent Monday and the death day of Danish would-be king Henrik 
        System.out.println(getPreviousWorkingDay(List.of(MonthDay.of(Month.FEBRUARY, 12), 
                MonthDay.of(Month.FEBRUARY, 13), MonthDay.of(Month.FEBRUARY, 14))));
    

    今天这印:

    2018_02_14
    2018_02_13
    2018_02_09
    

    (我因为星期一星期一不是每年的同一天而作弊;但我认为并不要求考虑这样的假期 . )

    由于确定今天的日期是时区敏感操作,如果它不是复活节岛时区,请替换您想要的时区 . 编辑:在Java 8中使用 Arrays.asList() 而不是Java 9 List.of() .

    Link: Oracle tutorial: Date Time解释如何使用 java.time .

  • 3

    Ole V.V.的回答很好 . 这里有一些增加这种方法的技巧 .

    EnumSet

    您可以将周末定义为周六和周日 DayOfWeekDayOfWeek 对象 . EnumSetSet 的高效实现,用于保存枚举对象 . 内存非常少,执行速度非常快 .

    Set<DayOfWeek> weekend = EnumSet.of( DayOfWeek.SATURDAY , DayOfWeek.SUNDAY ) ;
    

    然后询问该集合中是否包含日期的星期几 .

    boolean isWeekend = weekend.contains( localDate.getDayOfWeek() ) ;
    

    ThreeTen-Extra

    ThreeTen-Extra项目使用其他功能扩展了java.time . 这些功能包括 TemporalAdjuster ,用于在跳过任何星期六或星期日时跳至next / previous日期 .

    LocalDate nextWeekDay = org.threeten.extra.Temporals.nextWorkingDay( localDate ) ;
    

    Custom TemporalAdjuster

    您可以编写自己的 TemporalAdjuster 实现,将所有周末假日逻辑封装在一个可以通过简单的紧凑调用轻松重用的地方 .

    LocalDate nextBusinessDay = localDate.with( com.example.Temporals.nextBusinessDay() ) ;
    

相关问题