java实现日历效果的示例代码

 更新时间:2023年12月05日 10:02:54   作者:唐 昊  
这篇文章主要为大家详细介绍了如何使用java实现打印某年全部的日历信息,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以学习一下

使用java实现打印某年全部的信息

示例代码

import java.util.Calendar;
import java.util.GregorianCalendar;

public class shuz {

    public static int[][] calendarArray(int year,int month) {


        // 创建Calendar对象并设置日期为2023年8月1日
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, 1);

        // 获取2023年8月的天数
        int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        // 创建一个二维数组来存储日历
        int[][] calendarArray = new int[6][7];

        // 将日历中的日期存储到二维数组中
        /**
         *  calendar.get(Calendar.DAY_OF_WEEK) 为开始的坐标
         *      从星期日开始, 那么2为周一
         *      那么从星期一开始,那么2为
         */
        int dif_between;
        if (calendar.get(Calendar.DAY_OF_WEEK) == 1){
            dif_between = 6;
        }else {
            dif_between = calendar.get(Calendar.DAY_OF_WEEK) - 2;
        }

        for (int i = 1; i <= days; i++) {
            int nowVal = i + dif_between - 1;
            int row =   nowVal / 7 ;
            int column = nowVal % 7;
            calendarArray[row][column] = i;
        }

        boolean firstRowEmpty = isFirstRowEmpty(calendarArray);

        if (firstRowEmpty){
            calendarArray = removeEmptyFirstRow(calendarArray);

        }
        // 打印日历
//        System.out.println("一\t二\t三\t四\t五\t六\t日");
        // 打印日历
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 7; j++) {
                int val = calendarArray[i][j];
//                System.out.print(calendarArray[i][j] + "\t");
            }
        }
        return calendarArray;
    }

    /**
     *  判断是否第一行为空
     * @param array
     * @return
     */
    public static boolean isFirstRowEmpty(int[][] array) {
        for (int i = 0; i < array[0].length; i++) {
            if (array[0][i] != 0) {
                return false;
            }
        }
        return true;
    }

    /**
     *  转置数据,将第一行数据为空的数组转置
     *   例如: 000
     *         111
     *   改为:
     *         111
     *         000
     * @param array
     * @return
     */
    public static int[][] removeEmptyFirstRow(int[][] array) {
        if (array.length == 0 || array[0].length == 0) {
            return array;
        }
        int[][] processedArray = new int[6][7];
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 7; j++) {
                processedArray[i][j] = 0;
            }
        }
        for (int i = 1; i < array.length; i++) { // Start from the second row
            processedArray[i - 1] = array[i]; // Remove the first row and add the rest to the new array
        }
        return processedArray;
    }
    /**
     * 根据当前日期的起始时间,
     * @param args
     */
    public static void main(String[] args) {
        for (int k = 0; k < 12; k++) {

        int[][] ints = handel_data(306000000,k);
            System.out.println(k + 1 + "月");

        System.out.println("一\t二\t三\t四\t五\t六\t日");
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 7; j++) {
                System.out.print(ints[i][j] + "\t");
            }
            System.out.println();
        }
        }
    }

    private static int[][] handel_data(int year,int month){
//        int year = 2023;
        // 月份是从0开始的,所以7代表八月

        // 如果当天是星期天,那么前面会有一些空白,因为每个月的第一天不一定是星期天
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, 1);
        /**
         *  星期日 1 - 6 (1 - 1)% 7
         *  星期一 2 - 1
         *  星期二 3 - 2
         *  星期三 4 - 3
         *  星期四 5 - 4
         *  星期五 6 - 5
         *  星期六 7 - 6
         *
         *
         *  那么从星期一开始 则为7
         */
        int dif_between;
        if (calendar.get(Calendar.DAY_OF_WEEK) == 1){
            dif_between = 6;
        }else {
            dif_between = calendar.get(Calendar.DAY_OF_WEEK) - 2;
        }
        int[][] calendarArray = handle_before(year,month-1 ,dif_between,calendarArray(year,month));
        return handle_after(calendarArray);
    }

    private static int[][] handle_before(int year, int month,int bew,int[][] arr) {
        GregorianCalendar calendar = new GregorianCalendar();
        // 设置年份和月份
        // 设置日期和时间
        calendar.set(year, month, 1); // 设置年份、月份和日期

        // 获取这个月的总天数
        int maxDayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        int begin_day = maxDayOfMonth  - bew + 1;

        int[] data = getData(begin_day, maxDayOfMonth , bew);
        for (int i = 0; i < data.length; i++) {
            arr[0][i] = data[i];
        }
        return arr;
    }

    private static int[][] handle_after(int[][] calendarArray) {
        int index = 1;
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 7; j++) {
                int val = calendarArray[i][j];
                if (val == 0){
                    calendarArray[i][j] = index;
                    index++;
                }
            }
        }
        return calendarArray;
    }

    private static int[] getData( int start,int end,int maxDayOfMonth){

        int[] res = new int[maxDayOfMonth+1];
        for (int i = 1; i <= maxDayOfMonth; i++) {
            res[i] = i;
        }
        int[] resVal = new int[end-start + 1];
        int index = 0;
        while (start <= end){
            resVal[index] = start;
            index++;
            start++;
        }
        return resVal;

    }
}

输出:

1月
一 二 三 四 五 六 日
25 26 27 28 29 30 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 1 2 3 4 5
2月
一 二 三 四 五 六 日
30 31 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 1 2
3 4 5 6 7 8 9
3月
一 二 三 四 五 六 日
29 30 31 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 1 2
3 4 5 6 7 8 9
4月
一 二 三 四 五 六 日
25 26 27 28 29 30 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 1 2 3 4 5
5月
一 二 三 四 五 六 日
31 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 1 2 3 4
5 6 7 8 9 10 11
6月
一 二 三 四 五 六 日
27 28 29 30 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
1 2 3 4 5 6 7
7月
一 二 三 四 五 六 日
26 27 28 29 30 31 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 1 2 3 4 5
8月
一 二 三 四 五 六 日
30 31 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 1 2 3 4 5
6 7 8 9 10 11 12
9月
一 二 三 四 五 六 日
24 25 26 27 28 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 1 2 3 4 5 6
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 1 2 3 4 5
6 7 8 9 10 11 12
11月
一 二 三 四 五 六 日
28 29 30 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 1
2 3 4 5 6 7 8
12月
一 二 三 四 五 六 日
27 28 29 30 31 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
1 2 3 4 5 6 7

Process finished with exit code 0

到此这篇关于java实现日历效果的示例代码的文章就介绍到这了,更多相关java日历内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详解SpringBoot Mybatis如何对接多数据源

    详解SpringBoot Mybatis如何对接多数据源

    这篇文章主要为大家介绍了SpringBoot Mybatis如何对接多数据源实现方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • Java及nginx实现文件权限控制代码实例

    Java及nginx实现文件权限控制代码实例

    这篇文章主要介绍了Java及nginx实现文件权限控制代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • Spring @Conditional注解示例详细讲解

    Spring @Conditional注解示例详细讲解

    @Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件给容器注册bean,这篇文章主要介绍了Spring @Conditional注解示例详细讲解,需要的朋友可以参考下
    2022-11-11
  • 双重检查锁定模式Java中的陷阱案例

    双重检查锁定模式Java中的陷阱案例

    这篇文章主要介绍了双重检查锁定模式Java中的陷阱,双重检查锁定(也叫做双重检查锁定优化)是一种软件设计模式,它的作用是减少延迟初始化在多线程环境下获取锁的次数,尤其是单例模式下比较突出,想具体了解的小伙伴可以参考下面文章内容,附呦详细的举例说明
    2021-10-10
  • Mybatis 查询语句条件为枚举类型时报错的解决

    Mybatis 查询语句条件为枚举类型时报错的解决

    这篇文章主要介绍了Mybatis 查询语句条件为枚举类型时报错的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Java中redisTemplate注入失败NullPointerException异常问题解决

    Java中redisTemplate注入失败NullPointerException异常问题解决

    这篇文章主要介绍了Java中redisTemplate注入失败NullPointerException异常问题解决,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2023-08-08
  • Java 生成PDF文档的示例代码

    Java 生成PDF文档的示例代码

    这篇文章主要介绍了Java 生成PDF文档的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • 总结一些Java常用的加密算法

    总结一些Java常用的加密算法

    今天给大家带来的是关于Java的相关知识,文章围绕着Java加密算法展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06
  • SpringBoot项目在IntelliJ IDEA中如何实现热部署

    SpringBoot项目在IntelliJ IDEA中如何实现热部署

    spring-boot-devtools是一个为开发者服务的一个模块,其中最重要的功能就是自动应用代码更改到最新的App上面去。,这篇文章主要介绍了SpringBoot项目在IntelliJ IDEA中如何实现热部署,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • springboot+WebMagic+MyBatis爬虫框架的使用

    springboot+WebMagic+MyBatis爬虫框架的使用

    本文是对spring boot+WebMagic+MyBatis做了整合,使用WebMagic爬取数据,然后通过MyBatis持久化爬取的数据到mysql数据库。具有一定的参考价值,感兴趣的可以了解一下
    2021-08-08

最新评论