开发者

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

开发者 https://www.devze.com 2023-12-06 10:23 出处:网络 作者: 唐 昊
使用Java实现打印某年全部的信息 示例代码 import java.util.Calendar; import java.util.GregorianCalendar;

使用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 = nowVaandroidl % 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[]http://www.devze.com[] 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
         */
        intwww.devze.com dif_between;
        if js(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日历内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号