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對象并設(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) 為開始的坐標
* 從星期日開始, 那么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();
// 設(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)文章
Spring中的@Conditional注解實現(xiàn)分析
這篇文章主要介紹了Spring中的@Conditional注解實現(xiàn)分析, @Conditional是Spring 4出現(xiàn)的注解,但是真正露出價值的是Spring Boot的擴展@ConditionalOnBean等,需要的朋友可以參考下2023-12-12
在?Java?中將Object?轉(zhuǎn)換為?Int的四種方法
這篇文章主要介紹了在Java中如何將?Object?轉(zhuǎn)換為Int,本文研究了在?Java中將Object轉(zhuǎn)換為int的四種不同方法,結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-05-05
knife4j+springboot3.4異常無法正確展示文檔
本文主要介紹了knife4j+springboot3.4異常無法正確展示文檔,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
springboot執(zhí)行延時任務(wù)之DelayQueue實例
這篇文章主要介紹了springboot執(zhí)行延時任務(wù)之DelayQueue實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02

