導航:首頁 > 源碼編譯 > java日歷演算法

java日歷演算法

發布時間:2024-10-24 14:24:45

java程序日歷記事本中:日歷.set(month, month-1, 1);裡面的參數是什麼意思

我們知道DAY_OF_WEEK是獲取當前日期是一周中的第幾天,而一周從周日算起,因此當我們取得這個值之後,為了要正確的截取出隨後的字元串中的那個中文字元,因此它需要減1來達到目的。你只要跟蹤一下程序運行,觀察變數的賦值就會理解它這樣編寫的用意了。

Ⅱ 用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 ( 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();
}
}

給你

閱讀全文

與java日歷演算法相關的資料

熱點內容
4台伺服器怎麼連接一個顯示器 瀏覽:502
重慶前端程序員私活app排行 瀏覽:560
櫻花跑酷地圖id加密碼 瀏覽:637
安卓怎麼動不動就更新軟體 瀏覽:877
pdf的意思 瀏覽:721
怎麼檢查fpga編譯錯誤 瀏覽:204
python編程usb 瀏覽:282
終端編譯器python 瀏覽:596
日線漲幅python 瀏覽:806
安卓手機怎麼調出應用 瀏覽:954
k大的教程文件解壓密碼 瀏覽:126
文件夾製作手機支架視頻 瀏覽:592
保密文件夾華為 瀏覽:251
java日歷演算法 瀏覽:304
鹿醫app群聊聊天記錄怎麼刪除 瀏覽:88
poop手機設置伺服器P地址 瀏覽:674
安卓手機怎麼傳到蘋果電腦上 瀏覽:729
建築智能化系統pdf 瀏覽:371
截斷的二進制指數退避演算法 瀏覽:723
干設計還是程序員 瀏覽:841