最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實現(xiàn)日歷效果的示例代碼

 更新時間:2023年12月05日 10:02:54   作者:唐 昊  
這篇文章主要為大家詳細(xì)介紹了如何使用java實現(xiàn)打印某年全部的日歷信息,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的小伙伴可以學(xué)習(xí)一下

使用java實現(xiàn)打印某年全部的信息

示例代碼

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

public class shuz {

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


        // 創(chuàng)建Calendar對象并設(shè)置日期為2023年8月1日
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, 1);

        // 獲取2023年8月的天數(shù)
        int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        // 創(chuàng)建一個二維數(shù)組來存儲日歷
        int[][] calendarArray = new int[6][7];

        // 將日歷中的日期存儲到二維數(shù)組中
        /**
         *  calendar.get(Calendar.DAY_OF_WEEK) 為開始的坐標(biāo)
         *      從星期日開始, 那么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;
    }

    /**
     *  轉(zhuǎn)置數(shù)據(jù),將第一行數(shù)據(jù)為空的數(shù)組轉(zhuǎn)置
     *   例如: 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;
    }
    /**
     * 根據(jù)當(dāng)前日期的起始時間,
     * @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代表八月

        // 如果當(dāng)天是星期天,那么前面會有一些空白,因為每個月的第一天不一定是星期天
        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();
        // 設(shè)置年份和月份
        // 設(shè)置日期和時間
        calendar.set(year, month, 1); // 設(shè)置年份、月份和日期

        // 獲取這個月的總天數(shù)
        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

到此這篇關(guān)于java實現(xiàn)日歷效果的示例代碼的文章就介紹到這了,更多相關(guān)java日歷內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用IDEA配置Maven搭建開發(fā)框架ssm教程

    使用IDEA配置Maven搭建開發(fā)框架ssm教程

    這篇文章主要為大家詳細(xì)介紹了使用IDEA配置Maven搭建開發(fā)框架ssm教程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • 最新評論

    榆树市| 葫芦岛市| 罗江县| 昌邑市| 怀柔区| 巨鹿县| 彭泽县| 瓦房店市| 崇左市| 桃源县| 常德市| 盐池县| 乾安县| 公安县| 东海县| 张掖市| 集安市| 汤原县| 秦皇岛市| 梅河口市| 峡江县| 海林市| 吴江市| 醴陵市| 拉孜县| 班戈县| 三明市| 东辽县| 兴安县| 嘉定区| 黔东| 马龙县| 习水县| 兴安县| 东港市| 华蓥市| 宁安市| 宁陕县| 溆浦县| 六盘水市| 萨嘎县|