package com.xxm.TaskA_Calendar;
import java.util.Calendar;
import java.io.*;
import java.util.ArrayList;
public class A_Calendar {
public static void main(String[] args) throws IOException {
PrintCalendar.printTitle();
lo:
while (true) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println();
System.out.print("请输入年份:");
int year = Integer.parseInt(reader.readLine());
if (year == -1) {
System.out.println("程序退出,谢谢使用");
break;
}
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
boolean leapYearOrNot;
if (year % 400 == 0) {
leapYearOrNot = true;
} else if (year % 100 == 0) {
leapYearOrNot = false;
} else if (year % 4 == 0) {
leapYearOrNot = true;
} else {
leapYearOrNot = false;
}
System.out.print("请输入月份:");
int month = 0;
int days = 0;
month = Integer.parseInt(reader.readLine());
if (month == -1) {
System.out.println("程序退出,谢谢使用");
break;
}
else if (month > 12 || month < 1) {
System.out.println("输入错误,请输入正确的月份");
continue;
} else if (month == 2 && leapYearOrNot) {
days = 29;
} else if (month == 2) {
days = 28;
} else if (((month == 1 || month == 3) || ((month == 5) || (month == 7)) || ((month == 8 || month == 10) || month == 12))) {
days = 31;
} else {
days = 30;
}
cal.set(Calendar.MONTH, month - 1);
cal.setFirstDayOfWeek(Calendar.MONDAY);
int dayOfWeek;
dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
PrintCalendar printCal = new PrintCalendar();
ArrayList<Integer> dateList = new ArrayList<>(days);
for (int i = 0; i < days; i++) {
dateList.add(i + 1);
}
printCal.printDayOfWeekTitle();
printCal.printDate(dateList, dayOfWeek);
}
}
static class PrintCalendar {
public static void printTitle() {
for (int i = 0; i < 53; i++) {
System.out.print('*');
}
System.out.println('*');
System.out.print('*');
for (int i = 0; i < 52; i++) {
System.out.print(' ');
}
System.out.println('*');
System.out.print('*');
for (int i = 0; i < 23; i++) {
System.out.print(' ');
}
System.out.print("CSDN万年历");
for (int i = 0; i < 20; i++) {
System.out.print(' ');
}
System.out.println('*');
System.out.print('*');
for (int i = 0; i < 52; i++) {
System.out.print(' ');
}
System.out.println('*');
for (int i = 0; i < 54; i++) {
System.out.print('*');
}
System.out.println();
System.out.println();
}
private void printBlankSpace() {
System.out.print(" ");
}
public void printDayOfWeekTitle() {
printBlankSpace();
System.out.print("一");
printBlankSpace();
System.out.print("二");
printBlankSpace();
System.out.print("三");
printBlankSpace();
System.out.print("四");
printBlankSpace();
System.out.print("五");
printBlankSpace();
System.out.print("六");
printBlankSpace();
System.out.print("日");
printBlankSpace();
System.out.println();
}
void printDate(ArrayList<Integer> dateList, int dayOfWeek) {
int oneToSevenVersionDayOfWeek;
if (dayOfWeek == 1) {
oneToSevenVersionDayOfWeek = 7;
} else {
oneToSevenVersionDayOfWeek = dayOfWeek - 1;
}
for (int i = 0; i < oneToSevenVersionDayOfWeek - 1; i++) {
dateList.add(0, 0);
}
ArrayList<String> dateStrings = new ArrayList<>(dateList.size() + oneToSevenVersionDayOfWeek - 1);
for (int i = 0; i < dateList.size(); i++) {
if (dateList.get(i) == 0) {
dateStrings.add(" ");
} else if (dateList.get(i) < 10) {
dateStrings.add(Integer.toString(dateList.get(i)) + " ");
} else {
dateStrings.add(Integer.toString(dateList.get(i)));
}
}
for (int i = 0; i < dateList.size(); i++) {
if ((i + 1) % 7 == 1) {
printBlankSpace();
System.out.print(dateStrings.get(i));
printBlankSpace();
}
else if ((i + 1) % 7 == 6) {
System.out.print("\033[31;m" + dateStrings.get(i) + "\033[0m");
printBlankSpace();
}
else if ((i + 1) % 7 == 0) {
System.out.print("\033[31;m" + dateStrings.get(i) + "\033[0m");
printBlankSpace();
System.out.println();
} else {
System.out.print(dateStrings.get(i));
printBlankSpace();
}
}
}
}
}
实现效果如图:
|