导航:首页 > 源码编译 > 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日历算法相关的资料

热点内容
广州程序员薪资 浏览:888
win7命令提示符输入 浏览:591
关于程序员的故事 浏览:482
吃鸡服务器是什么样的 浏览:19
为什么pspice没有单片机 浏览:5
高数第六版下册答案pdf 浏览:903
sm3算法实现java 浏览:673
只有程序员才懂的笑话 浏览:265
php开发app接口源码 浏览:12
加密狗宝宝 浏览:237
单片机抢答器的原理 浏览:928
百利达体脂仪可以用什么app连接 浏览:226
服务器2016自动备份怎么取消 浏览:844
dos攻击命令大全 浏览:842
梦世界服务器怎么扩大领地 浏览:972
解压breathe 浏览:437
webview调用java 浏览:352
android待机流程 浏览:860
python直接退出程序 浏览:852
百战程序员收费标准 浏览:778