• JAVA打印markdown格式日历


    最近使用了markdown写东西,写日历的时候复制粘贴一行一行费劲,写了个程序生成如下:

    程序如下:

    package com.umf.hao;
    
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    import java.util.Scanner;
    
    /**
     * @ClassName: MyCalendarTable
     * @description Java生成markdown格式日历
     * @author haoprogrammer@hotmail.com
     * @time 2020年2月27日 下午3:01:09
     */
    public class MyCalendarTable {
        
    public static void main(String[] args) {
            
            Scanner scanner=new Scanner(System.in);
            System.out.print("<!-- 输入年份:  -->");
            int year = scanner.nextInt();
            System.out.print("<!--  输入月份: -->");
            int month = scanner.nextInt();
    
            System.out.println("##  " + year + "年"+ month + "月日历如下:
    ");
            
            String heads[]= {"日期","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
    
            tableHead(heads);
            //打印表格内容:
            tableBody(year, month);
        }
        
        /**
         * @Title: tableHead
         * @Description: 打印日历头部
         * @author haoprogrammer@hotmail.com
         * @time 2020年2月26日 下午4:51:37
         * @param heads
         * @return
         */
        public static void tableHead(String[] heads){
            //打印表格头
            for(int i = 0;i < heads.length; i ++){
                System.out.print("|" + heads[i] );
            }
            //标题行结束
            System.out.print("|
    ");
            for(int i = 0;i < heads.length; i ++){
                System.out.print("|-------");
            }
            //markdown语法行结束
            System.out.print("|
    ");
        }
        
        /**
         * @Title: tableBody
         * @Description: 打印日历体正文
         * @author haoprogrammer@hotmail.com
         * @time 2020年2月26日 下午4:52:57
         * @param year
         * @param month
         * @return
         */
        public static void tableBody(int year, int month) {
            
            GregorianCalendar cal=new GregorianCalendar(year ,month - 1,1);//对年份,月份,以及第一天来创建对象
            int totalDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);//获取该月份的天数
            int startDay = cal.get(Calendar.DAY_OF_WEEK) - 1;//获取该月的第一天是星期几
            System.out.print("| 日期    ");
            for(int i = 0;i < startDay;i ++) {
                System.out.print("|-------");  //输出第一天前markdown语法
            }
            
            for(int day = 1;day <= totalDays;day ++) {
                
                System.out.print("|   "+ day + "   ");
                startDay ++;
                if(startDay % 7 == 0) {  //每个星期输完换行
                    System.out.print("|
    ");
                    
                    System.out.print(makeString("已完成:") + "
    ");
                    System.out.print(makeString("未完成:") + "
    ");
                    System.out.print(makeString("需要沟通:") + "
    ");
                    System.out.print(makeString("其他:") + "
    ");
                    
                    System.out.print("| 日期    ");
                }
                if(day >= totalDays) {
                    System.out.print("
    " + makeString("已完成:") + "
    ");
                    System.out.print(makeString("未完成:") + "
    ");
                    System.out.print(makeString("需要沟通:") + "
    ");
                    System.out.print(makeString("其他:") + "
    ");
                }
            }
        }
    
        /**
         * @Title: makeString
         * @Description: 制作一行以key 开头的markdown字符串
         * @author haoprogrammer@hotmail.com
         * @time 2020年2月27日 下午2:45:58
         * @param key
         * @return
         */
        public static String makeString(String key) {
            StringBuilder work = new StringBuilder();
            work.append("|" + key);
            for(int i = 0; i < 8; i++) {
                work.append("|-------");
            }
            work.append("|");
            return work.toString();
        }    
        
    }

    生成效果如下:

  • 相关阅读:
    C#发送邮件简单例子
    ABAP随笔
    日期格式转换
    正则校验金额,整数8位,小数3位。
    angular语法运用技巧
    Oracle中连接与加号(+)的使用
    含有代码分析的面试题
    面试的java题目
    递归查询
    本地没有ORACLE远程登录oracle服务器
  • 原文地址:https://www.cnblogs.com/haoprogrammer/p/12394490.html
Copyright © 2020-2023  润新知