當前位置:編程學習大全網 - 編程語言 - 用java程序編壹個某年某月的日歷

用java程序編壹個某年某月的日歷

package mycalendar;

import java.util.*;

class ViewMonth {

int month;

int year;

ViewMonth(final int displayMonth, final int displayYear) {

month = displayMonth;

year = displayYear;

}

private String checkMonth() {

String[] months = {

"1 月" , "2 月" , "3 月",

"4 月" , "5 月" , "6 月",

"7 月" , "8 月" , "9 月",

"10 月" , "11 月" , "12 月"

};

return months[month];

}

private int checkDays() {

int[] numofDays = {

31, 28, 31,

30, 31, 30,

31, 31, 30,

31, 30, 31

};

return numofDays[month];

}

/**

* 使用此方法打印該月的日歷.

*/

void printMonth() {

/* 將該月份起始處的天數留空. */

int initialSpaces = 0;

try {

/* 獲取月份名稱. */

String monthName = checkMonth();

System.out.println();

System.out.println("\t\t\t " + year + " 年 " + monthName );

System.out.println();

} catch (ArrayIndexOutOfBoundsException ae) {

System.out.println("超出範圍 ..........");

System.exit(0);

}

GregorianCalendar cal = new GregorianCalendar(year, month, 1);

System.out.println("\t日\t壹\t二\t三\t四\t五\t六");

initialSpaces = cal.get(Calendar.DAY_OF_WEEK) - 1;

/* 獲取天數. */

int daysInMonth = checkDays();

/* 檢查是否為閏年並為二月增加壹天. */

if (cal.isLeapYear(cal.get(Calendar.YEAR)) && month == 1) {

++daysInMonth;

}

for (int ctr = 0; ctr < initialSpaces; ctr++) {

System.out.print("\t");

}

for (int ctr = 1; ctr <= daysInMonth; ctr++) {

/* 為單個日期添加空格. */

if (ctr <= 9) {

System.out.print(" ");

}

System.out.print("\t" + ctr);

/* 檢查行的末尾. */

if ((initialSpaces + ctr) % 7 == 0) {

System.out.println();

} else {

System.out.print(" ");

}

}

System.out.println();

}

}

class J7上機2 {

protected J7上機2() {

}

public static void main(String[] args) {

int month, year;

if (args.length == 2) {

System.out.println("顯示日歷");

System.out.println();

int mon = Integer.parseInt(args[0]);

month = mon - 1;

year = Integer.parseInt(args[1]);

} else {

Calendar today = Calendar.getInstance();

month = today.get(Calendar.MONTH);

year = today.get(Calendar.YEAR);

}

ViewMonth mv = new ViewMonth(month, year);

mv.printMonth();

}

}

給妳

  • 上一篇:法證先鋒二裏wilson的真名叫什麽
  • 下一篇:如何勸人戒網癮?
  • copyright 2024編程學習大全網