用于Java 8 LocalDateTime的周末过滤器 [英] Weekend filter for Java 8 LocalDateTime
问题描述
我想写一个布尔值函数,如果给定的 LocalDateTime
落在两个特定的时间点之间,返回true,否则返回false。
具体而言,如果给定日期是格林威治标准时间周五晚上22点至格林尼治标准时间周日晚上23点之间,我想要一个 LocalDateTime
过滤器。
一个框架可能如下所示:
public boolean isWeekend(LocalDateTime dateTime){
//检查dateTime是否在星期五22:00 GMT和星期天23:00 GMT
// return ...之间?
$ b $ p
$ b 这基本上是一个周末过滤器,我想知道是否有一个简单的解决方案与新的Java 8时间库(或任何其他现有的过滤器方法)。
我知道如何检查星期几,小时等,但避免重新发明轮子。
解决方案 我已经写了一个小程序来实现这个功能
公共类TestWeekend {
private static final int FRIDAY = 5; $ {
$ b pre $
private static final int SATURDAY = 6;
private static final int SUNDAY = 7;
private static final Integer WEEKEND_START_FRIDAY_CUT_OFF_HOUR = 22;
private static final Integer WEEKEND_END_SUNDAY_CUT_OFF_HOUR = 23;
private static List< Integer> weekendDaysList = Arrays.asList(FRIDAY,SATURDAY,SUNDAY);
public static void main(String [] args)抛出FileNotFoundException {
System.out.println(is weekend - + isWeekend(LocalDateTime.of(2016,4,22,18 ,39)));
System.out.println(is weekend - + isWeekend(LocalDateTime.of(2016,4,22,21,59)));
System.out.println(is weekend - + isWeekend(LocalDateTime.of(2016,4,22,22,0)));
System.out.println(is weekend - + isWeekend(LocalDateTime.of(2016,4,23,5,0)));
System.out.println(is weekend - + isWeekend(LocalDateTime.of(2016,4,24,8,0)));
System.out.println(is weekend - + isWeekend(LocalDateTime.of(2016,4,24,22,59)));
System.out.println(is weekend - + isWeekend(LocalDateTime.of(2016,4,24,23,0)));
System.out.println(is weekend - + isWeekend(LocalDateTime.of(2016,4,25,11,5)));
public static boolean isWeekend(LocalDateTime dateTime){
System.out.print(Date - + dateTime +,);
if(SATDDAY == dateTime.getDayOfWeek()。getValue()){
return true;
if(weekendDaysList.contains(dateTime.getDayOfWeek()。getValue())) (FRIDAY == dateTime.getDayOfWeek()。getValue()&& dateTime.getHour()> = WEEKEND_START_FRIDAY_CUT_OFF_HOUR){
return true;
if
} else if(SUNDAY == dateTime.getDayOfWeek()。getValue()&& dateTime.getHour()< WEEKEND_END_SUNDAY_CUT_OFF_HOUR){
return true;
//检查dateTime是否在星期五22:00和星期日23:00 GMT之间。
return false;
}
}
I want to write a boolean valued function which returns true if the given LocalDateTime
falls between two specific points in time, false otherwise.
Specifically I want to have a LocalDateTime
filter if a given date is between 22:00 GMT Friday and 23:00 GMT Sunday.
A skeleton could look like this:
public boolean isWeekend(LocalDateTime dateTime) {
//Checks if dateTime falls in between Friday's 22:00 GMT and Sunday's 23:00 GMT
//return ...???
}
This is basically a weekend filter and I'm wondering if there is a simple solution with the new Java 8 Time library(or any other existing filter methods).
I know how check for day of week, hour etc. but avoid to reinvent the wheel.
I have written a small program to achieve this
PROGRAM
public class TestWeekend {
private static final int FRIDAY = 5;
private static final int SATURDAY = 6;
private static final int SUNDAY = 7;
private static final Integer WEEKEND_START_FRIDAY_CUT_OFF_HOUR = 22;
private static final Integer WEEKEND_END_SUNDAY_CUT_OFF_HOUR = 23;
private static List<Integer> weekendDaysList = Arrays.asList(FRIDAY, SATURDAY, SUNDAY);
public static void main(String []args) throws FileNotFoundException {
System.out.println(" is weekend - "+isWeekend(LocalDateTime.of(2016,4,22,18,39)));
System.out.println(" is weekend - "+isWeekend(LocalDateTime.of(2016,4,22,21,59)));
System.out.println(" is weekend - "+isWeekend(LocalDateTime.of(2016,4,22,22,0)));
System.out.println(" is weekend - "+isWeekend(LocalDateTime.of(2016,4,23,5,0)));
System.out.println(" is weekend - "+isWeekend(LocalDateTime.of(2016,4,24,8,0)));
System.out.println(" is weekend - "+isWeekend(LocalDateTime.of(2016,4,24,22,59)));
System.out.println(" is weekend - "+isWeekend(LocalDateTime.of(2016,4,24,23,0)));
System.out.println(" is weekend - "+isWeekend(LocalDateTime.of(2016,4,25,11,5)));
}
public static boolean isWeekend(LocalDateTime dateTime) {
System.out.print("Date - "+dateTime+" , ");
if(weekendDaysList.contains(dateTime.getDayOfWeek().getValue()) ){
if(SATURDAY == dateTime.getDayOfWeek().getValue()){
return true;
}
if(FRIDAY == dateTime.getDayOfWeek().getValue() && dateTime.getHour() >=WEEKEND_START_FRIDAY_CUT_OFF_HOUR){
return true;
}else if(SUNDAY == dateTime.getDayOfWeek().getValue() && dateTime.getHour() < WEEKEND_END_SUNDAY_CUT_OFF_HOUR ){
return true;
}
}
//Checks if dateTime falls in between Friday's 22:00 GMT and Sunday's 23:00 GMT
return false;
}
}
这篇关于用于Java 8 LocalDateTime的周末过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!