Java判断一个日期是否在下周日期区间

Java实现判断一个日期是否在下周日期区间的方法

 1 /**
 2      * 判断输入的日期是否在下周区间
 3      * @return
 4      * @author nemowang
 5      */
 6     public static boolean isNextWeek(Date date) {
 7         // 设置时间格式
 8         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 9         Calendar calendar1 = Calendar.getInstance();
10         Calendar calendar2 = Calendar.getInstance();
11         int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
12         int offset1 = 1 - dayOfWeek;
13         int offset2 = 7 - dayOfWeek;
14         calendar1.add(Calendar.DATE, offset1 + 7);
15         calendar2.add(Calendar.DATE, offset2 + 7);
16         // 下周星期一
17         String monday = sdf.format(calendar1.getTime());
18         System.out.println(monday);
19         // 下周星期日
20         String sunday = sdf.format(calendar2.getTime());
21         System.out.println(sunday);
22 
23         String format = sdf.format(date);
24         try {
25             Date parse = sdf.parse(format);
26             Date mon = sdf.parse(monday);
27             Date sun = sdf.parse(sunday);
28             return parse.compareTo(mon) >= 0 && sun.compareTo(parse) >= 0;
29         } catch (ParseException e) {
30             e.printStackTrace();
31         }
32         return false;
33     }

 

posted @ 2019-08-17 14:39  NemoWang  阅读(925)  评论(0编辑  收藏  举报