java實(shí)現(xiàn)日歷效果的示例代碼
使用java實(shí)現(xiàn)打印某年全部的信息
示例代碼
import java.util.Calendar;
import java.util.GregorianCalendar;
public class shuz {
public static int[][] calendarArray(int year,int month) {
// 創(chuàng)建Calendar對(duì)象并設(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)建一個(gè)二維數(shù)組來(lái)存儲(chǔ)日歷
int[][] calendarArray = new int[6][7];
// 將日歷中的日期存儲(chǔ)到二維數(shù)組中
/**
* calendar.get(Calendar.DAY_OF_WEEK) 為開(kāi)始的坐標(biāo)
* 從星期日開(kāi)始, 那么2為周一
* 那么從星期一開(kāi)始,那么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)前日期的起始時(shí)間,
* @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開(kāi)始的,所以7代表八月
// 如果當(dāng)天是星期天,那么前面會(huì)有一些空白,因?yàn)槊總€(gè)月的第一天不一定是星期天
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
*
*
* 那么從星期一開(kāi)始 則為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è)置日期和時(shí)間
calendar.set(year, month, 1); // 設(shè)置年份、月份和日期
// 獲取這個(gè)月的總天數(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實(shí)現(xiàn)日歷效果的示例代碼的文章就介紹到這了,更多相關(guān)java日歷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring中的@Conditional注解實(shí)現(xiàn)分析
這篇文章主要介紹了Spring中的@Conditional注解實(shí)現(xiàn)分析, @Conditional是Spring 4出現(xiàn)的注解,但是真正露出價(jià)值的是Spring Boot的擴(kuò)展@ConditionalOnBean等,需要的朋友可以參考下2023-12-12
Springboot錯(cuò)誤頁(yè)面和錯(cuò)誤信息定制操作
這篇文章主要介紹了Springboot錯(cuò)誤頁(yè)面和錯(cuò)誤信息定制操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
在?Java?中將Object?轉(zhuǎn)換為?Int的四種方法
這篇文章主要介紹了在Java中如何將?Object?轉(zhuǎn)換為Int,本文研究了在?Java中將Object轉(zhuǎn)換為int的四種不同方法,結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
Java中高效的對(duì)象映射庫(kù)Orika的用法詳解
Orika是一個(gè)高效的Java對(duì)象映射庫(kù),專(zhuān)門(mén)用于在Java應(yīng)用程序中簡(jiǎn)化對(duì)象之間的轉(zhuǎn)換,下面就跟隨小編一起來(lái)深入了解下Orika的具體使用吧2024-11-11
Java?Date(日期)對(duì)象進(jìn)行格式化的思路詳解
Date類(lèi)是經(jīng)常會(huì)使用到的一個(gè)用來(lái)處理日期、時(shí)間的一個(gè)類(lèi)。Date類(lèi)是在java.util包下的Date類(lèi),這篇文章主要介紹了Java?Date(日期)對(duì)象如何進(jìn)行格式化呢,需要的朋友可以參考下2022-09-09
knife4j+springboot3.4異常無(wú)法正確展示文檔
本文主要介紹了knife4j+springboot3.4異常無(wú)法正確展示文檔,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
springboot執(zhí)行延時(shí)任務(wù)之DelayQueue實(shí)例
這篇文章主要介紹了springboot執(zhí)行延時(shí)任務(wù)之DelayQueue實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
淺談java7增強(qiáng)的try語(yǔ)句關(guān)閉資源
下面小編就為大家?guī)?lái)一篇淺談java7增強(qiáng)的try語(yǔ)句關(guān)閉資源。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
Java使用線(xiàn)程池執(zhí)行定時(shí)任務(wù)
本文介紹了Java使用線(xiàn)程池執(zhí)行定時(shí)任務(wù),其中ScheduledThreadPool和SingleThreadScheduledExecutor都是可以執(zhí)行定時(shí)任務(wù)的,但是具體怎么執(zhí)行,下面我們一起進(jìn)入文章了解具體詳情吧2022-05-05

