zoukankan      html  css  js  c++  java
  • 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();
        }    
        
    }

    生成效果如下:

  • 相关阅读:
    Webdriver启动Firefox浏览器后,页面显示空白
    Windows+Python+Selenium基础篇之1-环境搭建
    office2010无法卸载问题
    .NET使用FastDBF读写DBF
    并发编程相关概念
    C#的多线程简洁笔记
    asp.net core 3.1 入口:Program.cs中的Main函数
    【WPF学习】第四十四章 图画
    .NET知识梳理——1.泛型Generic
    C#个推SDK推送安卓+iOS
  • 原文地址:https://www.cnblogs.com/haoprogrammer/p/12394490.html
Copyright © 2011-2022 走看看