日历问题
package Lesson5;
import java.util.Scanner;
public class PrintCalender {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("输入年份:");
int year = input.nextInt();
System.out.print("输入月份:");
int month = input.nextInt();
System.out.println(getAllDay(month, year));
PrintMonth(month, year);
}
private static void PrintMonth(int month, int year) {
getMonthTital(month, year);
getMonthBody(month, year);
}
private static void getMonthTital(int month, int year) {
System.out.println(" " + getMonthName(month) + " " + year);
System.out.println("---------------------------");
System.out.println(" Sun" + " " + "Mon" + " " + "Tue" + " " + "Wed"
+ " " + "Thu" + " " + "Fri" + " " + "Sat");
}
private static String getMonthName(int month) {
String MonthName = "";
switch (month) {
case 1:
return "Jan";
case 2:
return "Feb";
case 3:
return "Mar";
case 4:
return "Apr";
case 5:
return "May";
case 6:
return "Jun";
case 7:
return "July";
case 8:
return "Aug";
case 9:
return "Sept";
case 10:
return "Oct";
case 11:
return "Nov";
case 12:
return "Dec";
}
return MonthName;
}
private static void getMonthBody(int month, int year) {
int startDay = getstartDay(month, year);
int monthDay = getMonthDay(month, year);
for (int i = 0; i < startDay; i++) {
System.out.print(" ");
}
for (int i = 1; i <= monthDay; i++) {
System.out.printf("%4d", i);
if ((i + startDay) % 7 == 0) {
System.out.println();
}
}
System.out.println();
}
private static int getstartDay(int month, int year) {
final int Start_Day_For_Jan_1_1800 = 3;
int allDay = getAllDay(month, year);
return (allDay + Start_Day_For_Jan_1_1800) % 7;
}
// private static int getAllDay(int month, int year) {
// int total = 0;
// for (int i = 1800; i < year; i++) {
// if (isLeapyear(i)) {
// total = total + 366;
// } else {
// total = total + 365;
// }
// }
for (int i = 1; i < month; i++) {
total = total + getMonthDay(month, i);
}
return total;
}
private static int getMonthDay(int month, int year) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12) {
return 31;
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
}
if (month == 2)
return isLeapyear(year) ? 29 : 28;
return 0;
}
private static boolean isLeapyear(int year) {
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
}
日历总是算不准 我怀疑是我的总天数算错了 求高手指教 总天数已经特别注释了
------解决方案--------------------
像这种日期日历,还是用Calendar来处理方便
修改一下getMonthBody方法,用Calendar来处理
Java code
private static void getMonthBody(int month, int year) {
//int startDay = getstartDay(month, year);
//int monthDay = getMonthDay(month, year);
Calendar c = Calendar.getInstance(); //获取Calendar实例
c.set(Calendar.YEAR, year); //设置年
c.set(Calendar.MONTH, month-1); //设置月
c.set(Calendar.DAY_OF_MONTH, 1); //设置月初
int startDay = c.get(Calendar.DAY_OF_WEEK)
- c.getActualMinimum(Calendar.DAY_OF_WEEK); //计算周开始
int monthDay = c.getActualMaximum(Calendar.DAY_OF_MONTH); //计算月末日
for (int i = 0; i < startDay; i++) {
System.out.print(" ");
}
for (int i = 1; i <= monthDay; i++) {
System.out.printf("%4d", i);
if ((i + startDay) % 7 == 0) {
System.out.println();
}
}
System.out.println();
}