Java练习:可视化日历小程序

public static void main(String[] args) throws ParseException {
        System.out.println("请输入日期(格式 2020-8-12)");
        Scanner scanner = new Scanner(System.in);
        String date = scanner.nextLine();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = dateFormat.parse(date);

        Calendar c = new GregorianCalendar();
        c.setTime(parse);
        int day = c.get(Calendar.DAY_OF_MONTH);
        int dayMax = c.getActualMaximum(Calendar.DATE);
        c.set(Calendar.DAY_OF_MONTH, 1);
        System.out.println("日\t一\t二\t三\t四\t五\t六");
        // 控制每月1号星期对齐
        for (int i = 0; i < c.get(Calendar.DAY_OF_WEEK) - 1; i++) {
            System.out.print("\t");
        }
        //输入日历
        for (int i = 0; i < dayMax; i++) {
            //判断如果打印的日期和输入日期相同后面加*
            if (c.get(Calendar.DAY_OF_MONTH) == day) {
                System.out.print(c.get(Calendar.DAY_OF_MONTH) + "*" + "\t");
            } else {
                System.out.print(c.get(Calendar.DAY_OF_MONTH) + "\t");
            }
            //如果日期为星期六换行
            if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
                System.out.println();
            }
            //当前日期+1
            c.add(Calendar.DAY_OF_MONTH, 1);
        }
    }

Java练习:可视化日历小程序_第1张图片

你可能感兴趣的:(Java,日历,Date,Calendar,SimpleFormat,DateFormat)