日期:2014-05-20  浏览次数:20787 次

帮我想想这个程序该怎么写?
给定出生日,计算与此对应的是星期几。

------解决方案--------------------
这个需要想吗?
------解决方案--------------------
import java.io.*;
class TheDay
{
public static void main(String[] args)
{
//System.out.println( "Hello World! ");
DataInputStream dis;
int year;
int month;
int day;
try
{
dis=new DataInputStream(System.in);
System.out.print( "Year: ");
year=Integer.parseInt(dis.readLine());
System.out.print( "Month: ");
month=Integer.parseInt(dis.readLine());
System.out.print( "Day: ");
day=Integer.parseInt(dis.readLine());
System.out.print( "这天是: ");
System.out.println(WeekDay.nameOfWeekday(year,month,day));

}
catch(IOException e)
{
System.out.println( "输入日期错误!!!!! ");
}

}
}
class WeekDay
{
/*public static int weekdayOfFirstDayOfTear(int year)
{
int tem=year-1;
return(tem+tem/4-tem/100+tem/400)%7;

}*/
public static boolean isRunNian(int year)
{
if (year%4==0&&year%100!=0)
return true;
else if(year%400==0)
return true;
else
return false;
}
public static int weekDay(int year,int month,int day)
{
//int firstDay=weekdayOfFirstDayOfTear(year);
int tem=year-1;
int monthDays[]=new int[12];
for(int i=0;i <7;i++)
{
if(i%2==0)
monthDays[i]=31;
else
monthDays[i]=30;
}
if(isRunNian(year))
monthDays[1]=29;
else
monthDays[1]=28;
for(int i=7;i <12;i++)
{
if(i%2==0)
monthDays[i]=30;
else
monthDays[i]=31;
}
int total=day;
for(int i=0;i <month-1;i++)
total+=monthDays[i];
return (total+tem+(int)(tem/4)-(int)(tem/100)+(int)(tem/400))%7;
}
public static String nameOfWeekday(int year,int month,int day)
{
int weekday=weekDay(year,month,day);
switch(weekday)
{

case 0:
return "Sunday ";
case 1:
return "Monday ";
case 2:
return "Tuesday ";
case 3:
return "Wendsday ";
case 4:
return "Thursday ";
case 5:
return "Friday ";
case 6:
return "Saturday ";

default:
return "Error ";
}
}

};
--------------------------
以前写的,你拿去看看
------解决方案--------------------
java.util Class Date
有个一个方法 getTime()
Returns:
the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this date.
也可以利用这个。



------解决方案--------------------
摆脱 用calendar类不好麽?
------解决方案--------------------
Calendar c = Calendar.getInstance();
c.get(Calendar.DAY_OF_WEEK)//用这个方法能得到星期几
------解决方案--------------------
public static int getWeek(String birthday) {
Calendar c = Calendar.getInstance();
c.set(Integer.parseInt(birthday.substring(0, 4)), Integer
.parseInt(birthday.substring(4, 6))-1, Integer.parseInt(birthday
.substring(6, 8)));
return c.get(Calendar.DAY_OF_WEEK) - 1;