• 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);
        }
    
    
    }

    ggband
  • 相关阅读:
    RHEL7.2安装及配置实验环境
    VMwareworkstation 12安装
    Iterator主要有三个方法:hasNext()、next()、remove()详解
    httpclient
    http接口测试——Jmeter接口测试实例讲解
    java获取Excel的导出
    java获取Excel的导入
    java的post请求
    java的get请求
    Python3 列表(List)基础
  • 原文地址:https://www.cnblogs.com/ggband/p/9561872.html
Copyright © 2020-2023  润新知