//已知 2000.1.1星期六
public class Wnl {
//1.判断某年是否为闰年
static boolean isLeapYear(int year){
return (year%4==0&&year%100!=0||year%400==0)?true:false;
}
//2.某年某月有多少天
static int yearMonthDays(int year,int month){
int yearMonthDays=0;
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
yearMonthDays=31;
break;
case 4:
case 6:
case 9:
case 11:
yearMonthDays=30;
break;
case 2:
if(isLeapYear(year))
yearMonthDays=29;
else
yearMonthDays=28;
}
// System.out.println(year+"年"+month+"月共有:"+yearMonthDays);
return yearMonthDays;
}
//3.某年有多少天
static int yearDays(int year){
int yearDays=isLeapYear(year)?366:365;
// System.out.println(year+"年有:"+yearDays);
return yearDays;
}
//4、某年某月第一天到2000.1.1多少天
static int yearMonthDayTo_2000_1_1(int year,int month){
int days=0;
//2012.4.1~2000.1.1
if(year>=2000){
for(int i=2000;i<year;i++){
days=days+yearDays(i);
}
for(int i=1;i<month;i++){
days=days+yearMonthDays(year,i);
}
}else{
//1988.5.1~2000.1.1
for(int i=year+1;i<2000;i++){
days=days+yearDays(i);
}
for(int i=month;i<=12;i++){
days=days+yearMonthDays(year,i);
}
}
return days;
}
//5.某年某月的第一天是星期几
//0 1 2 3 4 5 6
static int weekDay(int year,int month){
字符串截取日期 int weekDay=-1;
if(year>=2000){
weekDay=(6+yearMonthDayTo_2000_1_1(year,month))%7;
}else{
weekDay=6-yearMonthDayTo_2000_1_1(year,month)%7;
}
return weekDay;
}
//6.显示 某年某月的日历
static void display(int year,int month){
System.out.println();
System.out.println("\t\t"+year+"年"+month+"月");
System.out.println("日\t一\t二\t三\t四\t五\t六\t周");
int count=0;
for(int i=0;i<weekDay(year,month);i++){
count++;
System.out.print("\t");
}
for(int i=1;i<=yearMonthDays(year,month);i++){
count++;
System.out.print(i+"\t");
if(count%7==0){
System.out.println();
}
}
System.out.println();
}
//7.显示某年的日历
static void display2(int year){
for(int i=1;i<=12;i++){
display(year,i);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
display2(1994);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论