打印给定年月的日历
2014-07-17 08:28
316 查看
import java.util.Scanner; public class PrintCalendar { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("输入年份:"); int year = input.nextInt(); System.out.print("输入月份(1月--12月):"); int month = input.nextInt(); printMonth(year, month); } public static void printMonth(int year, int month) { printMonthTitle(year, month); printMonthBody(year, month); } public static void printMonthTitle(int year, int month) { System.out.println("\t\t\t" + getMonthName(month) + " " + year); System.out.println("----------------------------------"); System.out.println(" Sun Mon Tue Wed thu Fri Sat "); } public static void printMonthBody(int year, int month) { int starDay = getStartDay(year, month); int numberOfDaysInMonth = getNumberOfDayInMonth(year, month); int i = 0; int weeks = getNumberWeekDay(year, month); System.out.printf("%4d", weeks); for (i = 0; i < starDay; i++) System.out.print("\t"); for (i = 1; i <= numberOfDaysInMonth; i++) { System.out.printf("%4d", i); if ((i + starDay) % 7 == 0) { System.out.println(); weeks = weeks + 1; System.out.printf("%4d", weeks); } } System.out.println(); } public static String getMonthName(int month) { String monthName = ""; switch (month) { case 1: monthName = "January"; break; case 2: monthName = "February"; break; case 3: monthName = "March"; break; case 4: monthName = "April"; break; case 5: monthName = "May"; break; case 6: monthName = "June"; break; case 7: monthName = "July"; break; case 8: monthName = "August"; break; case 9: monthName = "September"; break; case 10: monthName = "October"; break; case 11: monthName = "November"; break; case 12: monthName = "December"; break; } return monthName; } /** * 获取每月一号是星期几 */ public static int getStartDay(int year, int month) { final int STAR_DAY_FOR_JAN_1_1800 = 3; int totalNumberOfDays = getTotalNumberOfDays(year, month); return (totalNumberOfDays + STAR_DAY_FOR_JAN_1_1800) % 7; } public static int getTotalNumberOfDays(int year, int month) { 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 + getNumberOfDayInMonth(year, i); return total; } /** * @param year * @param month * @return weeks 返回截止当月之前的有多少周 */ public static int getNumberWeekDay(int year, int month) { int totalDays = 0; int startDays = getStartDay(year, month); int weeks; for (int i = 1; i < month; i++) totalDays = totalDays + getNumberOfDayInMonth(year, i); if (startDays <= 4) { weeks = ((totalDays - (7 - startDays)) / 7 + 1); } else { weeks = ((totalDays - (7 - startDays)) / 7); } if (startDays != 0 && month != 1) { weeks++; } return weeks; /** * Created by zhouyubin on 7/15/14. */ } public static int getNumberOfDayInMonth(int year, int month) { 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; } public static boolean isLeapYear(int year) { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { return true; } return false; } } 运行效果: 输入年份:2014 输入月份(1月--12月):7 July 2014 ---------------------------------- Sun Mon Tue Wed thu Fri Sat 27 1 2 3 4 5 28 6 7 8 9 10 11 12 29 13 14 15 16 17 18 19 30 20 21 22 23 24 25 26 31 27 28 29 30 31 Process finished with exit code 0
相关文章推荐
- (Java)打印指定年月的日历
- 用java语言实现万年历(输入年月,打印当前日历,主要使用Java Swing技术)
- 打印任意年月的日历
- Java输入年月,打印该月的日历,已知2000年1月1号是星期六
- JAVA 按年月打印日历 Calendar的简单使用
- 用java语言实现万年历(输入年月,打印当前日历,主要使用Java Swing技术)
- C++ 输入年月,打印出这个月的日历
- 1.输入年月,打印该月的日历,同Windows右下角日历
- Java实现按年月打印日历功能【基于Calendar】
- 用java语言实现万年历(输入年月,打印当前日历,主要使用Java Swing技术)
- java----根据输入的年月判断闰年和打印日历
- java----根据输入的年月判断闰年和打印日历
- 使用shell实现打印给定日期的日历
- 用JAVA打印当前月份的日历
- (用二进制的思路)给定一个集合S,打印出集合所有的子集
- java打印日历---Calendar类的综合运用
- Java 打印日历
- SQL打印全年日历
- java实现输入指定年打印该年日历练习
- 输入年月,输出该月份的日历页