// 定义一个类来表示日历public class Calendar {// 定义一个数组来存储月份private String[] months = {"一月", "二月", "三月", "四月", "五月", "六月","七月", "八月", "九月", "十月", "十一月", "十二月"};// 定义一个数组来存储星期private String[] weeks = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};// 定义一个变量来存储当前年份private int year;// 定义一个变量来存储当前月份private int month;//this.month = date.getMonth() + 1;// 设置当前日子this.day = date.getDate();}// 定义一个方法来获取日历public String getCalendar() {// 创建一个字符串来存储日历StringBuilder calendar = new StringBuilder();// 添加年份和月份calendar.Append(this.year).append(" 年 ").append(this.months[this.month - 1]).append("\n");// 添加星期for (String week : this.weeks) {calendar.append(week).append(" ");}calendar.append("\n");// 添加日子// 计算当前月份的第一天是星期几int firstDayOfWeek = (this.year - 1) 365 + (this.month星期二", "星期三", "星期四", "星期五", "星期六"];// 创建一个字符串来存储日历var calendar = "";// 添加年份和月份calendar += year + " 年 " + months[month - 1] + "\n";// 添加星期for (var i = 0; i < weeks.length; i++) {calendar += weeks[i] + " ";}calendar += "\n";// 添加日子// 计算当前月份的第一天是星期几var firstDayOfWeek = (year - 1) 365 + (month - 1) 30 + day;firstDayOfWeek %= 7;// 添加空格for (var i = 0; i < firstDayOfWeek; i++) {calendar += " ";}// 添加日子for (var i = 1; i <= daysOfMonth(); i++) {// 如果是当前日期,则用方括号括起来if (i == day) {calendar += "[" + i + "] ";} else {calendar += i + " ";}// 如果是星期末,则换行if ((firstDayOfWeek + i) % 7 == 0) {calendar += "\n";}}// 返回日历return calendar;}// 定义一个函数来获取当前月份的天数function daysOfMonth() {// 根据月份获取天数switch (month) {case 2:return 28 + (year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ? 1 : 0);case 4:case 6:case 9:case 11:return 30;default:return 31;}}
本文地址:https://www.qianwe.cn/article/0e42071ba9aba2fc0b58.html
上一篇:日历代码javascript日历代码大全...
下一篇:日历代码日历代码大全...