美文网首页
控制台实现可视化日历

控制台实现可视化日历

作者: _MrWhite | 来源:发表于2020-02-01 16:33 被阅读0次
package visualizablecalender;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class TestCalender {
    public static void main(String[] args) throws ParseException {
        System.out.println("请输入日期(格式为:2010-12-12):");
        Scanner input = new Scanner(System.in);
        String strDate = input.nextLine();
        System.out.println("您刚刚输入的日期是:" + strDate);
        //以上内容都为边角料,下面才是真正的核心
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = simpleDateFormat.parse(strDate);

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(parse);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        calendar.set(Calendar.DAY_OF_MONTH, 1);

        System.out.println("日\t一\t二\t三\t四\t五\t六");

        int weeks = calendar.get(Calendar.DAY_OF_WEEK);

        for (int i = 1; i <weeks ; i++) {
            System.out.print("\t");
        }

        int actualMaximum = calendar.getActualMaximum(Calendar.DATE);
        for (int i = 1; i <= actualMaximum; i++) {
            if (i == day) {
                System.out.print(day + "*\t");
            } else {
                System.out.print(calendar.get(Calendar.DAY_OF_MONTH)+"\t");
            }
            if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
                System.out.println();
            }
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
    }
}

相关文章

网友评论

      本文标题:控制台实现可视化日历

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