欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

使用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對象并設置日期為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) 為開始的坐標
         *      從星期日開始, 那么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ù)當前日期的起始時間,
     * @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); // 設置年份、月份和日期

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

  • Spring中的@Conditional注解實現(xiàn)分析

    Spring中的@Conditional注解實現(xiàn)分析

    這篇文章主要介紹了Spring中的@Conditional注解實現(xiàn)分析,  @Conditional是Spring 4出現(xiàn)的注解,但是真正露出價值的是Spring Boot的擴展@ConditionalOnBean等,需要的朋友可以參考下
    2023-12-12
  • Springboot錯誤頁面和錯誤信息定制操作

    Springboot錯誤頁面和錯誤信息定制操作

    這篇文章主要介紹了Springboot錯誤頁面和錯誤信息定制操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 在?Java?中將Object?轉(zhuǎn)換為?Int的四種方法

    在?Java?中將Object?轉(zhuǎn)換為?Int的四種方法

    這篇文章主要介紹了在Java中如何將?Object?轉(zhuǎn)換為Int,本文研究了在?Java中將Object轉(zhuǎn)換為int的四種不同方法,結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2023-05-05
  • Java中高效的對象映射庫Orika的用法詳解

    Java中高效的對象映射庫Orika的用法詳解

    Orika是一個高效的Java對象映射庫,專門用于在Java應用程序中簡化對象之間的轉(zhuǎn)換,下面就跟隨小編一起來深入了解下Orika的具體使用吧
    2024-11-11
  • Java?Date(日期)對象進行格式化的思路詳解

    Java?Date(日期)對象進行格式化的思路詳解

    Date類是經(jīng)常會使用到的一個用來處理日期、時間的一個類。Date類是在java.util包下的Date類,這篇文章主要介紹了Java?Date(日期)對象如何進行格式化呢,需要的朋友可以參考下
    2022-09-09
  • Java之理解Redis回收算法LRU案例講解

    Java之理解Redis回收算法LRU案例講解

    這篇文章主要介紹了Java之理解Redis回收算法LRU案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • knife4j+springboot3.4異常無法正確展示文檔

    knife4j+springboot3.4異常無法正確展示文檔

    本文主要介紹了knife4j+springboot3.4異常無法正確展示文檔,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-01-01
  • springboot執(zhí)行延時任務之DelayQueue實例

    springboot執(zhí)行延時任務之DelayQueue實例

    這篇文章主要介紹了springboot執(zhí)行延時任務之DelayQueue實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 淺談java7增強的try語句關(guān)閉資源

    淺談java7增強的try語句關(guān)閉資源

    下面小編就為大家?guī)硪黄獪\談java7增強的try語句關(guān)閉資源。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Java使用線程池執(zhí)行定時任務

    Java使用線程池執(zhí)行定時任務

    本文介紹了Java使用線程池執(zhí)行定時任務,其中ScheduledThreadPool和SingleThreadScheduledExecutor都是可以執(zhí)行定時任務的,但是具體怎么執(zhí)行,下面我們一起進入文章了解具體詳情吧
    2022-05-05

最新評論