• Java简易日历的实现


    import java.text.DateFormat;
    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 VirtualCalendar {
    
        private static Scanner scanner;
    
        public static void main(String[] args) throws ParseException {
            try {
                System.out.println("请输入年份(例:2016)");
                scanner = new Scanner(System.in);
                int year = Integer.parseInt(scanner.nextLine());
                printCalendarOfAYear(year);
            } catch (ParseException e) {
                e.printStackTrace();
            }
    
        }
    
        /**
         * 打印星期
         */
        public static void printWeekLine() {
            System.out.println("日	一	二	三	四	五	六");
        }
        
        /**
         * 打印某一年所有月份的日历
         * @param year 年份
         * @throws ParseException
         */
        public static void printCalendarOfAYear(int year) throws ParseException {
            for (int i = 1; i <= 12; i++) {
                System.out.println("┍————————————————————————————————┑");
                System.out.println("♫			" + i + "月" + "			 ♫");
                System.out.println("┕————————————————————————————————┙");
                printWeekLine();
                printCalendarOfAMonth(year, i);
    
            }
        }
    
        /**
         * 打印某年某月的日历
         * @param year 年份
         * @param month 月份
         * @throws ParseException
         */
        public static void printCalendarOfAMonth(int year, int month)
                throws ParseException {
            DateFormat dateFormate = new SimpleDateFormat("yyyy-MM-dd");
            String dataString = year + "-" + month + "-" + "01";
            Date date = dateFormate.parse(dataString);
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            for (int i = 1; i <= days; i++) {
                int week = calendar.get(Calendar.DAY_OF_WEEK);
                if (i == 1) {
                    for (int j = 0; j < week - 1; j++) {
                        System.out.print("*	");
                    }
                }
                System.out.print(i);
                if (week == Calendar.SATURDAY) {
                    System.out.print("
    ");
                } else {
                    System.out.print("	");
                }
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }
            System.out.println();
            System.out.println();
        }
    }

    运行后的结果

    请输入年份(例:20162001
    ┍————————————————————————————————┑
    ♫            1月             ♫
    ┕————————————————————————————————┙
    日    一    二    三    四    五    六
    *    1    2    3    4    5    6
    7    8    9    10    11    12    13
    14    15    16    17    18    19    20
    21    22    23    24    25    26    27
    28    29    30    31    
    
    ┍————————————————————————————————┑
    ♫            2月             ♫
    ┕————————————————————————————————┙
    日    一    二    三    四    五    六
    *    *    *    *    1    2    3
    4    5    6    7    8    9    10
    11    12    13    14    15    16    17
    18    19    20    21    22    23    24
    25    26    27    28    
    
    ┍————————————————————————————————┑
    ♫            3月             ♫
    ┕————————————————————————————————┙
    日    一    二    三    四    五    六
    *    *    *    *    1    2    3
    4    5    6    7    8    9    10
    11    12    13    14    15    16    17
    18    19    20    21    22    23    24
    25    26    27    28    29    30    31
    
    
    ┍————————————————————————————————┑
    ♫            4月             ♫
    ┕————————————————————————————————┙
    日    一    二    三    四    五    六
    1    2    3    4    5    6    7
    8    9    10    11    12    13    14
    15    16    17    18    19    20    21
    22    23    24    25    26    27    28
    29    30    
    
    ┍————————————————————————————————┑
    ♫            5月             ♫
    ┕————————————————————————————————┙
    日    一    二    三    四    五    六
    *    *    1    2    3    4    5
    6    7    8    9    10    11    12
    13    14    15    16    17    18    19
    20    21    22    23    24    25    26
    27    28    29    30    31    
    
    ┍————————————————————————————————┑
    ♫            6月             ♫
    ┕————————————————————————————————┙
    日    一    二    三    四    五    六
    *    *    *    *    *    1    2
    3    4    5    6    7    8    9
    10    11    12    13    14    15    16
    17    18    19    20    21    22    23
    24    25    26    27    28    29    30
    
    
    ┍————————————————————————————————┑
    ♫            7月             ♫
    ┕————————————————————————————————┙
    日    一    二    三    四    五    六
    1    2    3    4    5    6    7
    8    9    10    11    12    13    14
    15    16    17    18    19    20    21
    22    23    24    25    26    27    28
    29    30    31    
    
    ┍————————————————————————————————┑
    ♫            8月             ♫
    ┕————————————————————————————————┙
    日    一    二    三    四    五    六
    *    *    *    1    2    3    4
    5    6    7    8    9    10    11
    12    13    14    15    16    17    18
    19    20    21    22    23    24    25
    26    27    28    29    30    31    
    
    ┍————————————————————————————————┑
    ♫            9月             ♫
    ┕————————————————————————————————┙
    日    一    二    三    四    五    六
    *    *    *    *    *    *    1
    2    3    4    5    6    7    8
    9    10    11    12    13    14    15
    16    17    18    19    20    21    22
    23    24    25    26    27    28    29
    30    
    
    ┍————————————————————————————————┑
    ♫            10月             ♫
    ┕————————————————————————————————┙
    日    一    二    三    四    五    六
    *    1    2    3    4    5    6
    7    8    9    10    11    12    13
    14    15    16    17    18    19    20
    21    22    23    24    25    26    27
    28    29    30    31    
    
    ┍————————————————————————————————┑
    ♫            11月             ♫
    ┕————————————————————————————————┙
    日    一    二    三    四    五    六
    *    *    *    *    1    2    3
    4    5    6    7    8    9    10
    11    12    13    14    15    16    17
    18    19    20    21    22    23    24
    25    26    27    28    29    30    
    
    ┍————————————————————————————————┑
    ♫            12月             ♫
    ┕————————————————————————————————┙
    日    一    二    三    四    五    六
    *    *    *    *    *    *    1
    2    3    4    5    6    7    8
    9    10    11    12    13    14    15
    16    17    18    19    20    21    22
    23    24    25    26    27    28    29
    30    31

    此示例主要使用Calendar 类及DataFormate列的子类的使用

  • 相关阅读:
    axios、ajax、fetch三者的区别
    React与Vue的相同与不同点
    react-redux
    redux【react】
    react高阶组件
    基于WebGL无插件虚拟场景漫游关键技术(完整版)ThingJS
    基于WebGL的三维交通监控可视化技术应用(实践版) ThingJS
    地下管线监控系统中互联网WebGL三维可视化构建技术 ThingJS
    基于WebGL实现智慧校园的全景漫游技术研究 三维可视化
    基于WebGL的3D可视化告警系统关键技术解析 ThingJS
  • 原文地址:https://www.cnblogs.com/doublejun/p/5570388.html
Copyright © 2020-2023  润新知