java实现日历效果的示例代码
使用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 12 3 4 5 6 7 89 10 11 12 13 14 1516 17 18 19 20 21 2223 24 25 26 27 28 2930 31 1 2 3 4 52月一 二 三 四 五 六 日30 31 1 2 3 4 56 7 8 9 10 11 1213 14 15 16 17 18 1920 21 22 23 24 25 2627 28 29 30 31 1 23 4 5 6 7 8 93月一 二 三 四 五 六 日29 30 31 1 2 3 45 6 7 8 9 10 1112 13 14 15 16 17 1819 20 21 22 23 24 2526 27 28 29 30 1 23 4 5 6 7 8 94月一 二 三 四 五 六 日25 26 27 28 29 30 12 3 4 5 6 7 89 10 11 12 13 14 1516 17 18 19 20 21 2223 24 25 26 27 28 2930 31 1 2 3 4 55月一 二 三 四 五 六 日31 1 2 3 4 5 67 8 9 10 11 12 1314 15 16 17 18 19 2021 22 23 24 25 26 2728 29 30 1 2 3 45 6 7 8 9 10 116月一 二 三 四 五 六 日27 28 29 30 1 2 34 5 6 7 8 9 1011 12 13 14 15 16 1718 19 20 21 22 23 2425 26 27 28 29 30 311 2 3 4 5 6 77月一 二 三 四 五 六 日26 27 28 29 30 31 12 3 4 5 6 7 89 10 11 12 13 14 1516 17 18 19 20 21 2223 24 25 26 27 28 2930 31 1 2 3 4 58月一 二 三 四 五 六 日30 31 1 2 3 4 56 7 8 9 10 11 1213 14 15 16 17 18 1920 21 22 23 24 25 2627 28 1 2 3 4 56 7 8 9 10 11 129月一 二 三 四 五 六 日24 25 26 27 28 1 23 4 5 6 7 8 910 11 12 13 14 15 1617 18 19 20 21 22 2324 25 26 27 28 29 3031 1 2 3 4 5 610月一 二 三 四 五 六 日1 2 3 4 5 6 78 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 30 1 2 3 4 56 7 8 9 10 11 1211月一 二 三 四 五 六 日28 29 30 1 2 3 45 6 7 8 9 10 1112 13 14 15 16 17 1819 20 21 22 23 24 2526 27 28 29 30 31 12 3 4 5 6 7 812月一 二 三 四 五 六 日27 28 29 30 31 1 23 4 5 6 7 8 910 11 12 13 14 15 1617 18 19 20 21 22 2324 25 26 27 28 29 301 2 3 4 5 6 7Process finished with exit code 0
到此这篇关于java实现日历效果的示例代码的文章就介绍到这了,更多相关java日历内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论