美文网首页java学习笔记整理
根据当前日期获取本周、下一周的所有日期,获取当前周的第一天和最后

根据当前日期获取本周、下一周的所有日期,获取当前周的第一天和最后

作者: _借东西的小人 | 来源:发表于2021-05-08 10:56 被阅读0次
    public static void main(String[] args) {
            //https://www.jianshu.com/p/3f4101da9b95
            LocalDate listDays = LocalDate.now();
            //当前周的下一周
            LocalDate localDate = listDays.plusDays(7);
            Arrays.asList(DayOfWeek.values()).stream().map(listDays::with).collect(toList());
            System.out.println("本周所有日期:" + Arrays.asList(DayOfWeek.values()).stream().map(listDays::with).collect(toList()));
            System.out.println("下一周所有日期:" + Arrays.asList(DayOfWeek.values()).stream().map(localDate::with).collect(toList()));
    
            //当前周的第一天
            System.out.println(listDays.with(DayOfWeek.MONDAY));
            //当前周的最后一天
            System.out.println(listDays.with(DayOfWeek.SUNDAY));
            
            int value = listDays.getDayOfWeek().getValue();
            System.out.println("今天是本周的第几天:" + value);
    
            String weekDays = dateToWeek(String.valueOf(listDays));
            System.out.println("今天是周几:" + weekDays);
    
        }
    
        public static String dateToWeek(String datetime) {
            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
            String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
            // 获得一个日历
            Calendar cal = Calendar.getInstance();
            Date datet = null;
            try {
                datet = f.parse(datetime);
                cal.setTime(datet);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if (w < 0) {
                w = 0;
            }
            return weekDays[w];
        }
    

    相关文章

      网友评论

        本文标题:根据当前日期获取本周、下一周的所有日期,获取当前周的第一天和最后

        本文链接:https://www.haomeiwen.com/subject/tfcudltx.html