首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

帮小弟我想想这个程序该如何写

2012-01-18 
帮我想想这个程序该怎么写?给定出生日,计算与此对应的是星期几。[解决办法]这个需要想吗?[解决办法]import

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

[解决办法]
这个需要想吗?
[解决办法]
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;


}
随便写了一下,楼主自己再改改吧,思路也就这样
[解决办法]
import java.util.GregorianCalendar;
import java.util.Calendar;

public class XunZhaoRiQi{

private int a;

public XunZhaoRiQi(int nian , int yue ,int tian){

GregorianCalendar c = new GregorianCalendar(nian,yue-1,tian);
a = c.get(DAY_OF_WEEK)-1;=====> 指示当前一个星期中的某一天,即为星期几,因为GregorianCalendar默认是美国的星期算法,他们的是星期日为第一天,所以要减了一个才能知道是星期几.

}

public int getA(){

return a;

}

}

class SouXun{

XunZhaoRiQi a = new XunZhaoRiQi();
  System.out.println(a.getA());

}
还有其他的很多代表常量,你可以在Calendar类里面找到,或者查看API文档,地址是util里面的Calendar抽象类.
楼主该结帖了,好些天啦,发分吧.

热点排行