- // 获得当前日期与本周日相差的天数
- private static int getMondayPlus(Date gmtCreate) {
- Calendar cd = Calendar.getInstance();
- cd.setTime(gmtCreate);
- // 获得今天是一周的第几天,星期日是第一天,星期二是第三天......
- int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
-
星期日为一周的第一天 SUN MON TUE WED THU FRI SAT DAY_OF_WEEK返回值 1 2 3 4 5 6 7 - if (dayOfWeek == 6) { //这是周日了
- return 0;
- } else {
- return 1 - dayOfWeek; //距离第七天还有—几天 。
- }
- }
- //定义一个时间
- DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date gmtCreate = dateFormat2.parse("2018-07-08 22:36:01"); - // 获得下周星期一的日期
- public static Date getNextMonday(Date gmtCreate) {
- int mondayPlus = getMondayPlus(gmtCreate);
- GregorianCalendar currentDate = new GregorianCalendar();
- currentDate.setTime(gmtCreate);
- currentDate.add(GregorianCalendar.DATE, mondayPlus + 7);
- Date monday = currentDate.getTime();
- return monday;
- }
Calendar 获取下周一的日期
最新推荐文章于 2023-12-04 16:59:25 发布