有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java哪一年的日期与原始年份相同?

这是我在这个网站上的第一个问题,但我总是觉得这个网站非常有用
我的问题是:

  1. 你要求对方给出日期(例如填写日期[dd-mm-yyyy]: 2013年10月16日)
  2. 你不必问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 &lt; 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的日期和日历功能。。。我对前景做了很多错事。。

共 (2) 个答案

  1. # 1 楼答案

    对于给定的日期dd-MM-yyxx,一个简单的公式就是

    ( dd + m + xx + (xx/4) + (yy%4) ) % 7
     % is modulus operator which is remainder in general
    

    上面的答案会告诉你一周中的哪一天,即0:Mon 1:Tue。。。。6为太阳
    这里

    dd - Date given
    m - month value which is shown down in list calculated with MM value yy - first two digits of supplied year
    xx - last two digits of year

    现在,m值计算是

    • 1月和10月为0
    • 5月1日
    • 8月2日
    • 2月、3月和11月3日
    • 6月4日
    • 9月和12月5日
    • 7月和4月6日

    记住如果提供的月份是一月或二月,而提供的年份是闰年,则从上表中的m值中减去1,即-1表示一月,2表示二月
    闰年计算是

    if (yyyy % 4 == 0)
    {
       if( yyyy % 100 == 0)
       {
          return (yyyy % 400) == 0;
       }
       else
         return true;     
    }
    

    我希望你能完成剩下的编程工作
    这将帮助您找到提供日期的星期几,现在您只需要为所有年份添加循环

  2. # 2 楼答案

    你不能用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);
        }