/***输出X年X月X日,是星期几。*@authorOther**/importjava.util.Scanner;publicclassT8demo{/***getYear得到年*@return年*/publicstaticintgetYear(){Scannerinput=newScanner(Syste...
/**
* 输出X年X月X日,是星期几。
* @author Other
*
*/
import java.util.Scanner;
public class T8demo {
/**
* getYear 得到年
* @return 年
*/
public static int getYear(){
Scanner input=new Scanner(System.in);
int year=0;
System.out.print("年:");
year=input.nextInt();
return year;
}
/**
* getMonth 得到月
* @return 月
*/
public static int getMonth(){
Scanner input=new Scanner(System.in);
int month=0;
System.out.print("月:");
month=input.nextInt();
return month;
}
/**
*getDay 得到日
* @return 日
*/
public static int getDay(){
Scanner input=new Scanner(System.in);
int day=0;
System.out.print("日:");
day=input.nextInt();
return day;
}
/**
* isLeap 判断输入的年份是否是闰年
* @param year
* @return 是否
*/
public static boolean isLeap(int year){
boolean flag=false;
if((year%4==0&&year%100!=0)||(year%400==0)){
flag=true;
}
return flag;
}
/**
* getDaysOfYear 得到输入的日期是这年的第几天
* @param year
* @param month
* @param day
* @return
*/
public static int getDaysOfyear(int year,int month,int day){
int days=0;
int[] daysOfMonth={31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeap(year)){
daysOfMonth[1]=29;
}
int sumMonthOfDays=0;
for(int i=0;i
sumMonthOfDays=sumMonthOfDays+daysOfMonth[i];
}
days=sumMonthOfDays+day;
return days;
}
/**
* getAlldays 得到1900到输入年份的总天数
* @return 总天数
*/
public static int getAllDays(int year){
int days=0;
int yeardays1,yeardays2;
if(year>1900){
yeardays1=year;
yeardays2=1900;
}
else{
yeardays1=1900;
yeardays2=year;
}
for(;yeardays2
if((yeardays2%4==0&&yeardays2%100!=0)||(yeardays2%400==0)){
days+=366;}
else {
days+=365;}
}
return days;
}
/**
* getAllDays 得到输入的年月日离1900年1月1日的总天数
* @return 天
*/
public static int getAllDays(){
int days=0;
days=getAllDays(getYear())+getDaysOfyear(getYear() ,getMonth(),getDay());
return days;
}
public static void main(String[] args) {
int alldays=getAllDays();
String[] week=new String[]{"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
int i=0;
if(alldays%7==0){
i=0;}
else if(alldays%7==1){
i=1;}
else if(alldays%7==2){
i=2;
}
else if(alldays%7==3){
i=3;
}
else if(alldays%7==4){
i=4;
}
else if(alldays%7==5){
i=5;
}
else if(alldays%7==6){
i=6;
}System.out.println(getYear()+"年"+getMonth()+"月"+getDay()+"日是"+week[i]);
}
}
展开