java学习笔记整理

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

2021-05-08  本文已影响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];
    }
上一篇下一篇

猜你喜欢

热点阅读