下面我来详细讲解一下如何使用Java控制台打印本月的日历。
1. 获取当前系统日期
在Java中,可以通过 java.util.Calendar
类获取当前系统日期。该类默认获取的日期是当前系统日期,可以使用以下代码进行获取:
Calendar cal = Calendar.getInstance();
2. 获取本月天数并确定第一天
通过 Calendar
类获取到的当前日期,可以使用 getActualMaximum
方法获取本月的天数,并使用 set
方法将当前日期设置为本月的第一天。
cal.set(Calendar.DATE, 1);
int monthDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
3. 创建日历输出格式
日历的输出格式可以使用字符串拼接的方式进行创建。例如,创建一个格式为:
Sun Mon Tue Wed Thu Fri Sat
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
可以使用以下代码进行创建:
String[] weekDays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
String[][] days = new String[6][7];
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 7; i++) {
builder.append(weekDays[i]).append(" ");
}
builder.append("\n");
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
builder.append(String.format("%2s", days[i][j] == null ? "" : days[i][j])).append(" ");
}
builder.append("\n");
}
4. 填充日历输出格式
根据第一天的星期数,可以计算出当前日历输出格式中的第一个空位应该填充哪个日期。据此,可以使用一个二维数组来保存本月每一天的日期,然后按照星期数和日期填充到输出格式中。
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1;
for (int i = 0; i < monthDays; i++) {
int row = (i + dayOfWeek) / 7;
int col = (i + dayOfWeek) % 7;
days[row][col] = String.valueOf(i + 1);
}
5. 完整代码示例
下面是一个完整的Java控制台打印本月日历的示例代码:
import java.util.Calendar;
public class CalendarPrinter {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, 1);
int monthDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
String[] weekDays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
String[][] days = new String[6][7];
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 7; i++) {
builder.append(weekDays[i]).append(" ");
}
builder.append("\n");
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1;
for (int i = 0; i < monthDays; i++) {
int row = (i + dayOfWeek) / 7;
int col = (i + dayOfWeek) % 7;
days[row][col] = String.valueOf(i + 1);
}
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
builder.append(String.format("%2s", days[i][j] == null ? "" : days[i][j])).append(" ");
}
builder.append("\n");
}
System.out.println(builder.toString());
}
}
6. 示例输出
以下是该代码的示例输出:
Sun Mon Tue Wed Thu Fri Sat
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
7. 更多示例
下面是使用不同年份和月份的代码示例,可自行参考:
示例1:2021年6月
import java.util.Calendar;
public class CalendarPrinter {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2021);
cal.set(Calendar.MONTH, Calendar.JUNE);
cal.set(Calendar.DATE, 1);
int monthDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
String[] weekDays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
String[][] days = new String[6][7];
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 7; i++) {
builder.append(weekDays[i]).append(" ");
}
builder.append("\n");
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1;
for (int i = 0; i < monthDays; i++) {
int row = (i + dayOfWeek) / 7;
int col = (i + dayOfWeek) % 7;
days[row][col] = String.valueOf(i + 1);
}
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
builder.append(String.format("%2s", days[i][j] == null ? "" : days[i][j])).append(" ");
}
builder.append("\n");
}
System.out.println(builder.toString());
}
}
输出结果:
Sun Mon Tue Wed Thu Fri Sat
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
示例2:2022年2月
import java.util.Calendar;
public class CalendarPrinter {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2022);
cal.set(Calendar.MONTH, Calendar.FEBRUARY);
cal.set(Calendar.DATE, 1);
int monthDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
String[] weekDays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
String[][] days = new String[6][7];
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 7; i++) {
builder.append(weekDays[i]).append(" ");
}
builder.append("\n");
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1;
for (int i = 0; i < monthDays; i++) {
int row = (i + dayOfWeek) / 7;
int col = (i + dayOfWeek) % 7;
days[row][col] = String.valueOf(i + 1);
}
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
builder.append(String.format("%2s", days[i][j] == null ? "" : days[i][j])).append(" ");
}
builder.append("\n");
}
System.out.println(builder.toString());
}
}
输出结果:
Sun Mon Tue Wed Thu Fri Sat
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
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java控制台打印本月的日历 - Python技术站