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列的子类的使用

原文地址:https://www.cnblogs.com/doublejun/p/5570388.html