java哪一年的日期与原始年份相同?
这是我在这个网站上的第一个问题,但我总是觉得这个网站非常有用
我的问题是:
- 你要求对方给出日期(例如填写日期[dd-mm-yyyy]:
2013年10月16日)
- 你不必问2之间的间隔
年(例如给出一个间隔[yyyy-yyy]:1800-2000)
当程序运行时,它必须显示给定日期是星期几。这次是星期三。在这段时间间隔内,10月16日的日期也在周三
所以最后它必须看起来像这样:
Fill in a date: [dd-mm-yyyy]: 16-10-2013
Give an interval [yyyy-yyyy]: 1900-2000
16 October was a wednesday in the following years:
1905 1911 1916 1922 1933 1939 1944 1950 1961 1967 1972 1978 1989 1995 2000
完整日期为2013年10月16日星期三
最大的问题是,我不允许约会。函数在java中运行
如果有人能帮我做第二部分,我会非常高兴,因为我不知道该怎么做
为了找出给定日期在一周中的哪一天,我使用Zeller同余
class Day {
Date date; //To grab the month and year form the Date class
//In this class I check whether the giving date is in the correct form
int day;
int year1; //First interval number
int year2; //Second interval number
final static String[] DAYS_OF_WEEK = {
"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday"
};
public void dayWeekInterval{
int interval.year1;
int interval.year2;
for(int i = year1; year1 =< year2; year1++) {
//check if the day of the week in the giving year, is the same as the
//original year.
}
}
public void dayOfTheWeek {
int m = date.getMonth();
int y = date.getYear();
if (m < 3) {
m += 12;
y -= 1;
}
int k = y % 100;
int j = y / 100;
int day = ((q + (((m + 1) * 26) / 10) + k + (k / 4) + (j / 4)) +
(5 * j)) % 7;
return day;
}
public string ToString(){
return "" + DAYS_OF_WEEK[day] + day;
}
嘿,我把密码改了一点,但我不知道怎么打领带。我还忘了提到,我不允许使用java的日期和日历功能。。。我对前景做了很多错事。。
# 1 楼答案
对于给定的日期dd-MM-yyxx,一个简单的公式就是
上面的答案会告诉你一周中的哪一天,即0:Mon 1:Tue。。。。6为太阳
这里
现在,
m
值计算是记住如果提供的月份是一月或二月,而提供的年份是闰年,则从上表中的
m
值中减去1,即-1表示一月,2表示二月闰年计算是
我希望你能完成剩下的编程工作
这将帮助您找到提供日期的星期几,现在您只需要为所有年份添加循环
# 2 楼答案
你不能用
Date
,但你能用Calendar
吗?那么这就是你的代码: