leapYearOrNot
/** * 区分平年和闰年 */
public static boolean leapYearOrNot(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return true;
} else {
return false;
}
}
PrintTitle()
public static void PrintTitle() {
System.out.println("******************************************************");
System.out.println("* *");
System.out.println("* *");
System.out.println("* *");
System.out.println("* 万年历 *");
System.out.println("* *");
System.out.println("* *");
System.out.println("* *");
System.out.println("******************************************************");
}
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;
}
/** * 创建日历对象,并将year赋值进去给YEAR */
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
System.out.print("请输入月份:");
int mouth = 0;
int days = 0;
mouth = Integer.parseInt(reader.readLine());
if (mouth == -1) {
System.out.println("欢迎使用,已退出万年历程序,再见!");
break;
} else if (mouth >= 12 && mouth <= 9) {
System.out.println("您输入的月份不正确,请输入1-12的数字");
} else if (mouth == 2 && leapYearOrNot(year)) {
days = 29;
} else if (mouth == 2) {
days = 28;
} else if (mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12) {
days = 31;
} else {
days = 30;
}
/** * 设置输入的月份,但是要记住这里要减一! */
cal.set(Calendar.MONTH, mouth - 1);
//判断该月份的一天是星期几。注意:dayOfWeek = 1 表示周日,dayOfWeek = 2 表示周一,以此类推。
cal.setFirstDayOfWeek(Calendar.MONDAY);
int dayofweek;
dayofweek = cal.get(Calendar.DAY_OF_WEEK);
PrintCalendar
:static class PrintCalendar {
printBlankSpace()
printDayOfWeekTitle
public static 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();
}
printData
:public static void printData (ArrayList < Integer > datalist,int dayofWeek){
/** * 转换为中国星期 */
int ChinadayofWeek;
if (dayofWeek == 1) {
ChinadayofWeek = 7;
} else {
ChinadayofWeek = dayofWeek - 1;
}
/** * 将多余的0传入数组,因为不是每个月第一天都是星期一 */
for (int i = 0; i < ChinadayofWeek - 1; i++) {
datalist.add(0, 0); //位置和元素都为0
}
ArrayList<String> dataStrings = new ArrayList<>(datalist.size() + ChinadayofWeek - 1);
for (int i = 0; i < datalist.size(); i++) {
if (datalist.get(i) == 0) {
dataStrings.add(" ");
} else if (datalist.get(i) < 10) {
dataStrings.add(Integer.toString(datalist.get(i)) + " "); //这里的Integer.toString是将日期的Integer类型转换为字符串打印输出
} else {
dataStrings.add(Integer.toString(datalist.get(i)));
}
}
for (int i = 0; i < datalist.size(); i++) {
if ((i + 1) % 7 == 1) {
printBlankSpace();
System.out.print(dataStrings.get(i));
printBlankSpace();
} else if ((i + 1) % 7 == 6) {
System.out.print("\033[33;4m" + dataStrings.get(i) + "\033[0m");
printBlankSpace();
} else if ((i + 1) % 7 == 0) {
System.out.print("\033[33;4m" + dataStrings.get(i) + "\033[0m");
printBlankSpace();
System.out.println();
} else {
System.out.print(dataStrings.get(i));
printBlankSpace();
}
}
/** * 打印环节-------------------------------------------------------------- * 打印环节-------------------------------------------------------------- * 打印环节-------------------------------------------------------------- */
/** * 打印输出日历 */
PrintCalendar printCalendar = new PrintCalendar();
//将数据存入本月日期数临时数组中
ArrayList<Integer> dataList = new ArrayList<>(days);
for (int i = 0; i < days; i++) {
dataList.add(i + 1);
}
/** * 打印一开始的星期 */
printCalendar.printDayOfWeekTitle();
//打印日期
printCalendar.printData(dataList,dayofweek);
package 任务A;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
/** * @author ${范涛之} * @Description * @create 2021-11-18 10:47 */
public class CalendarTest {
/** * 区分平年和闰年 */
public static boolean leapYearOrNot(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return true;
} else {
return false;
}
}
public static void main(String[] args) throws IOException {
/** * 打印标题 */
PrintCalendar.PrintTitle();
/** * 一直输入数据,直到输入1的时候退出 */
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;
}
/** * 创建日历对象,并将year赋值进去给YEAR */
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
/** * 打印提示语: */
System.out.print("请输入月份:");
int mouth = 0;
int days = 0;
mouth = Integer.parseInt(reader.readLine());
/** * 根据平年闰年判断不同月份 */
if (mouth == -1) {
System.out.println("欢迎使用,已退出万年历程序,再见!");
break;
} else if (mouth >= 12 && mouth <= 9) {
System.out.println("您输入的月份不正确,请输入1-12的数字");
} else if (mouth == 2 && leapYearOrNot(year)) {
days = 29;
} else if (mouth == 2) {
days = 28;
} else if (mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12) {
days = 31;
} else {
days = 30;
}
/** * 设置输入的月份,但是要记住这里要减一! */
cal.set(Calendar.MONTH, mouth - 1);
//判断该月份的一天是星期几。注意:dayOfWeek = 1 表示周日,dayOfWeek = 2 表示周一,以此类推。
cal.setFirstDayOfWeek(Calendar.MONDAY);
int dayofweek;
dayofweek = cal.get(Calendar.DAY_OF_WEEK);
/** * 打印环节-------------------------------------------------------------- * 打印环节-------------------------------------------------------------- * 打印环节-------------------------------------------------------------- */
/** * 打印输出日历 */
PrintCalendar printCalendar = new PrintCalendar();
//将数据存入本月日期数临时数组中
ArrayList<Integer> dataList = new ArrayList<>(days);
for (int i = 0; i < days; i++) {
dataList.add(i + 1);
}
/** * 打印一开始的星期 */
printCalendar.printDayOfWeekTitle();
//打印日期
printCalendar.printData(dataList,dayofweek);
}
}
/** * 打印输出图案 */
static class PrintCalendar {
public static void PrintTitle() {
System.out.println("******************************************************");
System.out.println("* *");
System.out.println("* *");
System.out.println("* *");
System.out.println("* 万年历 *");
System.out.println("* *");
System.out.println("* *");
System.out.println("* *");
System.out.println("******************************************************");
}
/** * 打印空格 */
public static void printBlankSpace () {
System.out.print(" ");
}
//打印星期数的抬头
public static 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();
}
/** * 这里的datalist指代的就是可以理解为一整行的长度,里面就包括了1号前面的空格和日期,list可以确定长度所以适合作为循环边界,dataStrings不太好空值长度 * * @param datalist * @param dayofWeek */
public static void printData (ArrayList < Integer > datalist,int dayofWeek){
/** * 转换为中国星期 */
int ChinadayofWeek;
if (dayofWeek == 1) {
ChinadayofWeek = 7;
} else {
ChinadayofWeek = dayofWeek - 1;
}
/** * 将多余的0传入数组,因为不是每个月第一天都是星期一 */
for (int i = 0; i < ChinadayofWeek - 1; i++) {
datalist.add(0, 0); //位置和元素都为0
}
/** * 为了显示好看我们需要在日期为单个的数字后面加一个空格,这里新建了一个辅助数组 * dataStrings里面存放的就是实际要打印输出的东西,这里的dataStrings一共会打印三种类型的数据 * 1:当日期是空的时候也就是我前面所说的不是每个月都是周一的对应1号,这时前面就需要两个空格, * 2:当日期是一位数的时候需要打印的就是一个空格加这个数字 * 3:当日期是两位数的时候就恰好去输出这个日期 * 总而言之就是每一个打印的都要保证两位数 */
ArrayList<String> dataStrings = new ArrayList<>(datalist.size() + ChinadayofWeek - 1);
for (int i = 0; i < datalist.size(); i++) {
if (datalist.get(i) == 0) {
dataStrings.add(" ");
} else if (datalist.get(i) < 10) {
dataStrings.add(Integer.toString(datalist.get(i)) + " "); //这里的Integer.toString是将日期的Integer类型转换为字符串打印输出
} else {
dataStrings.add(Integer.toString(datalist.get(i)));
}
}
/** * 遍历打印 */
for (int i = 0; i < datalist.size(); i++) {
if ((i + 1) % 7 == 1) {
printBlankSpace();
System.out.print(dataStrings.get(i));
printBlankSpace();
} else if ((i + 1) % 7 == 6) {
System.out.print("\033[33;4m" + dataStrings.get(i) + "\033[0m");
printBlankSpace();
} else if ((i + 1) % 7 == 0) {
System.out.print("\033[33;4m" + dataStrings.get(i) + "\033[0m");
printBlankSpace();
System.out.println();
} else {
System.out.print(dataStrings.get(i));
printBlankSpace();
}
}
}
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/justleavel/article/details/121443655
内容来源于网络,如有侵权,请联系作者删除!
leapYearOrNot
PrintTitle()
PrintCalendar
:printBlankSpace()
printDayOfWeekTitle
printData
: