Calendar打印日历

Posted ggband

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Calendar打印日历相关的知识,希望对你有一定的参考价值。

package com.example.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    @Test
    public void contextLoads() {

        setMonth("2018年02月");

    }

    private Date str2Date(String str) {
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月");
            return df.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 设置月份
     */
    private void setMonth(String Month) {

        Date month = str2Date(Month);
        boolean isCurrentMonth = false;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        //获取今天是多少号
        int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
        int todayWeekIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1;

        Date cM = str2Date(getMonthStr(new Date()));
        //判断是否为当月
        if (cM.getTime() == month.getTime()) {
            isCurrentMonth = true;
            int selectDay = currentDay;//当月默认选中当前日
        } else {
            isCurrentMonth = false;
            int selectDay = 0;
        }
        System.out.println("设置月份:" + getMonthStr(month) + "   今天" + currentDay + "号, 是否为当前月:" + isCurrentMonth);
        calendar.setTime(month);
        int dayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        //第一行1号显示在什么位置(星期几)
        int firstIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        int lineNum = 1;
        //第一行能展示的天数
        int firstLineNum = 7 - firstIndex;
        int lastLineNum = 0;
        int shengyu = dayOfMonth - firstLineNum;
        while (shengyu > 7) {
            lineNum++;
            shengyu -= 7;
        }
        if (shengyu > 0) {
            lineNum++;
            lastLineNum = shengyu;
        }
        System.out.println(getMonthStr(month) + "月一共有" + dayOfMonth + "天,第一天的索引是:" + firstIndex + "   有" + lineNum +
                "行,第一行" + firstLineNum + "个,最后一行" + lastLineNum + "个");

        int days[][] = new int[5][7];

        String weeks[] = {"星期-", "星期二", "星期三", "星期四", "星期五"};

        for (int day = 0; day < dayOfMonth; day++) {

            int column = (day + firstIndex) % 7;
            int row = (day + firstIndex) / 7;
            days[row][column] = day + 1;
        }

        for (int i = 0; i < weeks.length; i++) {

            System.out.print(weeks[i] + "	");

        }
        System.out.println();
        for (int i = 0; i < days.length; i++) {
            for (int j = 0; j < days[i].length; j++) {
                if (days[i][j] == 0)
                    System.out.print("	");
                else
                    System.out.print(days[i][j] + "	");
            }

            System.out.println();
        }


    }


    /**
     * 获取月份标题
     */
    private String getMonthStr(Date month) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月");
        return df.format(month);
    }


}

 

技术分享图片

 

以上是关于Calendar打印日历的主要内容,如果未能解决你的问题,请参考以下文章

在Java中如何用calendar类输入年和月份打印当月的日历

Java打印日历表

打印某一个月的日历

日历返回错误的月份

Python3-笔记-E-004-库-日历calendar

日历(Calendar)模块