//指定的日历年份
public static int year;
//指定的日历月份
public static int month;
//获得总天数
public static int getDaysFrom1900() {
int days = 0;
//统计从1900年到指定日历年份的前一年的天数
for (int i = 1900; i < year; i++) {
days += isLeapYear(i) ? 366 : 365;
}
//统计从一月到指定日历月份的前一个月的天数
for (int i = 1; i < month; i++) {
days += getDaysFromMonth(i);
}
return days;
}
//获取指定月份的天数
private static int getDaysFromMonth(int month) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 2:
return isLeapYear(year) ? 29 : 28;
default:
return 30;
}
}
//判断指定年份是平年还是闰年
private static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
}
显示:这里用计数器来计数,能计空格的数量
//显示日历
public static void show() {
//获取从1900年到指定月份前一个月的总天数
int totalDays = getDaysFrom1900();
//获取指定月份的天数
int daysOfMonth = getDaysFromMonth(month);
//获取指定月份第一天前面的空格数
int space = totalDays % 7;
//计数器
int count = 0;
//显示
System.out.println("一二三四五六日");
System.out.println("==========================");
for (int i = 0; i < space; i++) {
count++;
System.out.print("");
}
for (int i = 1; i <= daysOfMonth; i++) {
count++;
System.out.print(i + "");
if (count % 7 == 0) {
count = 0;
//换行
System.out.println();
}
}
}
public static void show(int y, int m) {
//获取当前系统时间
Calendar c = Calendar.getInstance();
//获取当前的日期
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
//设置指定的日期
c.set(Calendar.YEAR,y);
c.set(Calendar.MONTH,m-1);
c.set(Calendar.DAY_OF_MONTH, 1);
//获取指定月份的总天数
int dayOfMonth = c.getActualMaximum(Calendar.DAY_OF_MONTH);
//获取指定月份第一天的星期
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
//显示
System.out.println("一二三四五六日");
System.out.println("==========================");
dayOfWeek = dayOfWeek == 0 ? 7 : dayOfWeek;
//获取指定月份前面的空格数
int space = dayOfWeek - 1;
int count = 0;
for (int i = 0; i < space; i++) {
count++;
System.out.print("");
}
for (int i = 1; i <= dayOfMonth; i++) {
count++;
//判断是不是当前系统的日期时间
String date = (y == year && m == month && i == day) ?
"*" + i + "" : i + "";
System.out.print(date);
if (count % 7 == 0) {
count = 0;
System.out.println();
}
}
}
public static void show(int y, int m) {
//获取当前系统时间
LocalDate date = LocalDate.now();
//获取当前的日期
int year = date.getYear();
int month = date.getMonth().getValue();
int day = date.getDayOfMonth();
//设置指定的日期
LocalDate date1 = LocalDate.of(y,m,1);
//获取指定月份的总天数
int daysOfMonth = date1.getMonth()
.length(y % 4 == 0 && y % 100 != 0 || y % 400 == 0);
//获取指定月份前面的空格数
int space= date1.getDayOfWeek().getValue()-1;
System.out.println("一二三四五六日");
System.out.println("==========================");
int count = 0;
for (int i = 0; i < space; i++) {
count++;
System.out.print("");
}
for (int i = 1; i <= daysOfMonth; i++) {
count++;
//判断是不是当前系统的日期时间
String src = (y == year && m == month && i == day) ?
"*" + i + "" : i + "";
System.out.print(src);
if (count % 7 == 0) {
count = 0;
System.out.println();
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
int year = sc.nextInt();
System.out.println("请输入月份:");
int month = sc.nextInt();
show(year, month);
}