非假期的最后工作日期
last working date that is not holiday
我有以下 Holiday
class:
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();
}
}
鉴于我有 list
个 holidays
,我如何计算 return 在 getPreviousWorkingDay(...)
方法中的最后一个工作日,这将排除周六和周日?
我正在尝试导出要查找的最后一个文件日期,所以我正在尝试解决类似的问题
if (todayIsHoliday(listOfHolidays)) {
getPreviousWorkingDay(listOfHolidays);
}
so 如果当天是节假日,查看最后一个非节假日的日期,return 为字符串格式。
我不确定如何回头。请注意假期列表不仅限于周六和周日。它们是乡村假期,例如农历新年等
我为此使用 java 8,因此欢迎任何重构或改进:)
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.time
,现代 Java 日期和时间 API,正如评论中提到的,我建议您也这样做。让我们看看上面的方法:
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
.
Ole 的回答 V.V。很好。这里还有一些技巧可以增强这种方法。
枚举集
您可以将周末定义为周六和周日的 EnumSet
个 DayOfWeek
个对象。 EnumSet
是 Set
的高效实现,用于保存枚举对象。占用很少的内存,执行速度非常快。
Set<DayOfWeek> weekend = EnumSet.of( DayOfWeek.SATURDAY , DayOfWeek.SUNDAY ) ;
然后询问日期的 day-of-week 是否包含在该集合中。
boolean isWeekend = weekend.contains( localDate.getDayOfWeek() ) ;
ThreeTen-Extra
ThreeTen-Extra project extends the java.time with additional features. Among this features is a TemporalAdjuster
for moving to the next/previous 日期,同时跳过任何周六或周日。
LocalDate nextWeekDay = org.threeten.extra.Temporals.nextWorkingDay( localDate ) ;
自定义TemporalAdjuster
您可以编写自己的 TemporalAdjuster
实现,将所有周末 + 假期逻辑封装在一个地方,可以通过简单的紧凑调用轻松重用。
LocalDate nextBusinessDay = localDate.with( com.example.Temporals.nextBusinessDay() ) ;
我有以下 Holiday
class:
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();
}
}
鉴于我有 list
个 holidays
,我如何计算 return 在 getPreviousWorkingDay(...)
方法中的最后一个工作日,这将排除周六和周日?
我正在尝试导出要查找的最后一个文件日期,所以我正在尝试解决类似的问题
if (todayIsHoliday(listOfHolidays)) {
getPreviousWorkingDay(listOfHolidays);
}
so 如果当天是节假日,查看最后一个非节假日的日期,return 为字符串格式。
我不确定如何回头。请注意假期列表不仅限于周六和周日。它们是乡村假期,例如农历新年等
我为此使用 java 8,因此欢迎任何重构或改进:)
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.time
,现代 Java 日期和时间 API,正如评论中提到的,我建议您也这样做。让我们看看上面的方法:
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
.
Ole 的回答 V.V。很好。这里还有一些技巧可以增强这种方法。
枚举集
您可以将周末定义为周六和周日的 EnumSet
个 DayOfWeek
个对象。 EnumSet
是 Set
的高效实现,用于保存枚举对象。占用很少的内存,执行速度非常快。
Set<DayOfWeek> weekend = EnumSet.of( DayOfWeek.SATURDAY , DayOfWeek.SUNDAY ) ;
然后询问日期的 day-of-week 是否包含在该集合中。
boolean isWeekend = weekend.contains( localDate.getDayOfWeek() ) ;
ThreeTen-Extra
ThreeTen-Extra project extends the java.time with additional features. Among this features is a TemporalAdjuster
for moving to the next/previous 日期,同时跳过任何周六或周日。
LocalDate nextWeekDay = org.threeten.extra.Temporals.nextWorkingDay( localDate ) ;
自定义TemporalAdjuster
您可以编写自己的 TemporalAdjuster
实现,将所有周末 + 假期逻辑封装在一个地方,可以通过简单的紧凑调用轻松重用。
LocalDate nextBusinessDay = localDate.with( com.example.Temporals.nextBusinessDay() ) ;