这是我在这个网站上的第一个问题,但我总觉得这个网站非常有用 我的意思是:
当程序运行时,它必须显示给定日期的星期几。在这种情况下,这是一个星期三。比计划要查看哪一年,在间隔期间,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中使用DATE.function。
如果有人可以帮我解决第二部分,我真的很开心,因为我不知道我应该怎么做这件事
为了找出给定日期的星期几,我使用了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; }
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; }
答案 0 :(得分:1)
只是一个简单的公式,找到给定日期的日期dd - MM - yyxx是,
( dd + m + xx + (xx/4) + (yy%4) ) % 7
% is modulus operator which is remainder in general
上面的答案将告诉你星期几,即0:星期一:星期二...... 6对于太阳
这里,
dd - 给出日期
m - 使用MM值计算的列表中显示的月份值 yy - 提供年份的前两位数字 xx - 年份的最后两位数
现在,m
值计算是,
记住如果提供的月份是1月或2月,并且提供的年份是跳跃,则从上表中的m
值中减去1,即Jan为1,2月为2
闰年计算是
if (yyyy % 4 == 0)
{
if( yyyy % 100 == 0)
{
return (yyyy % 400) == 0;
}
else
return true;
}
我希望你能做的其他编程。
这将帮助您找到提供日期的星期几,现在您只需要为所有年份添加循环。
答案 1 :(得分:0)
您无法使用Date
,但可以使用Calendar
吗?那么这就是你的代码:
Calendar c = Calendar.getInstance();
c.set(2013, 9, 16); // month starts at zero
System.out.printf("Original date is: %tc\n", c);
int weekday = c.get(Calendar.DAY_OF_WEEK);
System.out.printf("Weekday of original date is [by number] %d\n", weekday);
for(int year = 1800; year < 2000; year++) {
c.set(Calendar.YEAR, year);
if(weekday == c.get(Calendar.DAY_OF_WEEK))
System.out.printf("%tc was same weekday!\n", c);
}