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

熱點內容
音頻採集單片機 瀏覽:590
加密管的優點 瀏覽:280
dock基礎命令 瀏覽:345
java編程愛好者 瀏覽:723
做外包程序員怎麼樣 瀏覽:865
程序員技術門檻 瀏覽:473
路由花生殼搭建web伺服器地址 瀏覽:541
小米傳送文件用什麼app 瀏覽:102
哪個領域演算法好 瀏覽:380
用命令行編譯java 瀏覽:677
筆趣閣app哪個是正版手機app 瀏覽:427
程序員這個工作好嗎 瀏覽:898
agps定位伺服器地址 瀏覽:659
用水做的解壓玩具怎麼做 瀏覽:418
安卓411能下載什麼 瀏覽:304
小海龜logo命令 瀏覽:493
java製作界面 瀏覽:895
台達plc編程電纜製作 瀏覽:249
30多歲當程序員 瀏覽:442
怎樣把表格轉換成pdf 瀏覽:514