導航:首頁 > 源碼編譯 > 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日歷演算法相關的資料

熱點內容
路由器多種加密方法 瀏覽:604
程序員阻止電腦自動彈出定位 瀏覽:166
如何做伺服器服務商 瀏覽:759
su剖切命令 瀏覽:726
devc編譯背景 瀏覽:209
學習單片機的意義 瀏覽:49
音頻演算法AEC 瀏覽:909
加密貨幣容易被盜 瀏覽:82
蘋果平板如何開啟隱私單個app 瀏覽:704
空調壓縮機一開就停止 瀏覽:528
如何下載虎牙app 瀏覽:847
日語年號的演算法 瀏覽:955
dev裡面的編譯日誌咋調出來 瀏覽:298
php函數引用返回 瀏覽:816
文件夾和文件夾的創建 瀏覽:259
香港加密貨幣牌照 瀏覽:838
程序員鼓勵自己的代碼 瀏覽:393
計算機網路原理pdf 瀏覽:752
吃雞國際體驗服為什麼伺服器繁忙 瀏覽:94
php中sleep 瀏覽:491