您的位置:首页 > 其它

日历

2016-05-14 10:40 435 查看
import java.util.Scanner;

public class Date {

public Date(){

Scanner sc = new Scanner(System.in);

System.out.println(“请输入年份: “);

int year = sc.nextInt();

System.out.println(“请输入月份: “);

int month = sc.nextInt();

int total = 0;
for(int i=1900;i<year;i++){
total += getDaysForYear(i);
}
for(int i=1;i<month;i++){
total += getDays(year,i);
}
total += 1;

int dayofweek = total % 7;

int sum = getDays(year,month);

System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
if(dayofweek != 0){
int sum1;
if(month != 1){
sum1 = getDays(year,month-1);
}else{
sum1 = getDays(year-1,12);// 31
}

int i = sum1 - dayofweek + 1;
while(i<=sum1){
System.out.print(i + "\t");
i++;
}
}

for(int i=1;i<=sum;i++){
if(dayofweek == 0 && i == 1){

}else if((i+dayofweek-1)%7 == 0){
System.out.println();
}
System.out.print(i + "\t");
}

if((dayofweek+sum)%7 != 0){
int sum2 = ((dayofweek+sum)/7+1)*7-(dayofweek+sum);
for(int i=1;i<=sum2;i++){
System.out.print(i + "\t");
}
}
System.out.println();
sc.close();
}

public static int getDays(int year,int month){
if(month ==1 ||month ==3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
return 31;
}else if(month == 4 || month == 6 || month == 9 || month == 11){
return 30;
}else{
if(isRun(year) == true){
return 29;
}else{
return 28;
}
}
}

public static int getDaysForYear(int year){
if(isRun(year) == true){
return 366;
}else{
return 365;
}
}

private static boolean isRun(int year) {
// TODO Auto-generated method stub
if(year%4 == 0 && year%100 != 0 || year%400 == 0){
return true;
}else{
return false;
}
}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: