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

熱點內容
中原找房app如何注銷 瀏覽:839
程序員思考高手 瀏覽:593
gcc編譯器是幹啥的 瀏覽:808
int在java中的長度 瀏覽:132
電腦必備的五個神仙軟體程序員 瀏覽:212
什麼系統屬於安卓 瀏覽:359
有什麼游戲安卓oppo可以玩 瀏覽:721
microchip編程 瀏覽:872
數控車床宏程序編程入門 瀏覽:651
如何查伺服器上的密碼 瀏覽:874
手機alipay文件夾刪除 瀏覽:801
浩辰cad命令行 瀏覽:351
移動加密思路 瀏覽:970
php手冊pdf 瀏覽:752
安卓手機推薦什麼鍵位 瀏覽:112
android登錄驗證碼 瀏覽:476
聯通雲伺服器管理賬號 瀏覽:309
蘋果7加密晶元壞了 瀏覽:516
壓縮包2g解壓後有多大 瀏覽:956
php人才管理系統 瀏覽:21