写了一个万年历的简单算法 可以查询从1900到2099年的1~12月份及输入日期的查询,给大家参考一下
import java.util.Scanner;
public class Calendar {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年份");
int year = (scanner.nextInt());
year = isTrueYear(year);
System.out.print("请输入月份");
int month = (scanner.nextInt());
month = isTrueMonth(month);
System.out.print("请输入日期");
int day = (scanner.nextInt());
System.out.print("\n");
int total = allDays(year, month, day);
total += allDays2Year(year);
int monthDay = monthDays(year, month);
day = isTrueDay(day, monthDay);
System.out.println(1900 + "年从1月1日~" + year + "年" + month + "月" + day + "日一共有:" + total + "天");
System.out.println(year + "年" + "一共有:" + allDaysOfYear(year) + "天");
System.out
.println(year + "年从1月1日~" + year + "年" + month + "月" + day + "日一共有:" + allDays(year, month, day) + "天");
System.out.println(year + "年" + month + "月" + day + "日" + sun(total % 7));
System.out.println("\t" + "\t" + year + "年" + month + "月");
System.out.println("日" + "\t" + "一" + "\t" + "二" + "\t" + "三" + "\t" + "四" + "\t" + "五" + "\t" + "六");
for (int i = 0; i < (total - day + 1) % 7; i++) {
System.out.print("\t");
}
for (int c = 1; c <= 7 - (total - day) % 7 - 1; c++) {
System.out.print(c + "\t");
}
System.out.print("\n");
int k = 0;
for (int d = 7 - (total - day) % 7; d <= monthDay; d++) {
System.out.print(d + "\t");
k++;
if (k % 7 == 0) {
System.out.println("");
}
}
scanner.close();
}
// 返回1900年从1月1号到输入日期一共有多少天
public static int allDays(int year, int month, int day) {
int total = 0;
switch ((int) month) {
case 12:
total += 30;
case 11:
total += 31;
case 10:
total += 30;
case 9:
total += 31;
case 8:
total += 31;
case 7:
total += 30;
case 6:
total += 31;
case 5:
total += 30;
case 4:
total += 31;
case 3:
if (isLeapMonth(year)) {
total += 29;
} else {
total += 28;
}
case 2:
total += 31;
case 1:
total += day;
break;
}
return total;
}
// 返回当月有多少天
public static int monthDays(int year, int month) {
int total = 0;
switch ((int) month) {
case 12:
total = 31;
break;
case 11:
total = 30;
break;
case 10:
total = 31;
break;
case 9:
total = 30;
break;
case 8:
total = 31;
break;
case 7:
total = 31;
break;
case 6:
total = 30;
break;
case 5:
total = 31;
break;
case 4:
total = 30;
break;
case 3:
total = 31;
break;
case 2:
if (isLeapMonth(year)) {
total = 29;
} else {
total = 28;
}
break;
case 1:
total = 31;
break;
}
return total;
}
// 返回星期
public static String sun(int i) {
String d1 = "";
switch ((int) i) {
case 0:
d1 = "星期日";
break;
case 6:
d1 = "星期六";
break;
case 5:
d1 = "星期五";
break;
case 4:
d1 = "星期四";
break;
case 3:
d1 = "星期三";
break;
case 2:
d1 = "星期二";
break;
case 1:
d1 = "星期一";
break;
}
return d1;
}
// 1900到到前一年的12月31日的总天数
public static int allDays2Year(int year) {
int total = 0;
for (int n = 1900; n < year; n++) {
if (isLeapMonth(n)) {
total += 366;
} else {
total += 365;
}
}
return total;
}
// 检测日期是否合法
public static int isTrueYear(int year) {
while (year < 1900 || year > 2099) {
System.out.println("输入的年份应该在1900~2099");
System.out.print("请从新输入年份");
year = (new Scanner(System.in).nextInt());
}
return year;
}
public static int isTrueMonth(int month) {
while (month < 1 || month > 12) {
System.out.println("输入的月份应该在1~12");
System.out.print("请从新输入月份");
month = (new Scanner(System.in).nextInt());
}
return month;
}
public static int isTrueDay(int day, int monthDay) {
while (day <= 0 || day > monthDay) {
System.out.println("输入的日期应该在" + "1 ~ " + monthDay + "之间");
System.out.print("请从新输入日期");
day = (new Scanner(System.in).nextInt());
}
return day;
}
// 判断是否是闰月
public static boolean isLeapMonth(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return true;
} else {
return false;
}
}
// 判断当年总天数
public static int allDaysOfYear(int year) {
int total = 0;
if (isLeapMonth(year)) {
total = 366;
} else {
total = 365;
}
return total;
}
}