Judging the day of the week based on an arbitrary date in JAVA

public static void main(String[] args) throws ParseException {
        String[] weekDays = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        //获取当前时间(yyyy-MM-dd)格式
        String date ="2023-04-24";
        //转换为Date类型
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse(date);
        //判断日期是星期几
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        System.out.println(weekDays[w]);
    }

Guess you like

Origin blog.csdn.net/ssghzxc/article/details/130154757