【问题标题】:Whether it is correct to take 01/01/0001 date as Monday?将 01/01/0001 日期作为星期一是否正确?
【发布时间】:2011-10-07 00:18:35
【问题描述】:

我正在编写一个程序来显示给定日期的日期。 (例如 1970 年 1 月 1 日的星期四。) 在尝试时我遇到了一些问题。

这是我的程序。

/*Concept: 
 The noOfDays variable will count the no of days since 01/01/0001. And this will be Day one(1). E.g 
 noOfDays = 365 for 01/12/0001 and noOfDays = 731 for 01/01/0003 and so on....
 Since I taken Monday as 01/01/0001,the day of the specific date can be achieved by switching(noOfDays%7)
 E.g. 365 % 7 = 1. So, the 365th day is monday...
*/

import java.util.Scanner;
class DayDisplayer
{
    long noOfDays;
    int month;
    int days;
    int year;
    Scanner scan;
    int countYear;
    int countMonth;

    DayDisplayer()
    {
        scan = new Scanner(System.in);
        noOfDays = 0;
        System.out.println("Enter the date please");

        days = scan.nextInt();
        month = scan.nextInt();
        year = scan.nextInt();

        System.out.println("Your Date is:  "+days+"/"+month+"/"+year);



    }

    boolean leapYearCheck(int year)
    {
        if( ((year%100 == 0) && (year%400 != 0)) || (year%4 != 0) )  // when it is divisible by 100 and not by 400.  
            return false;
        else
            return true;

    }

    boolean isThere31Days()
    {
        if ( ( (countMonth >> 3) ^ (countMonth & 1) ) == 1 ) 
            return true;
        else 
            return false;

    }

    void getDaysThatInDays()    
    {
        noOfDays += days;
    }

    void getDaysThatInMonth()
    {

        countMonth = 1;

        while(countMonth<month)
        {
            if( countMonth == 2 )
                if( leapYearCheck(year) == true)
                    noOfDays += 29;
                else 
                    noOfDays += 28;
            else
               if ( isThere31Days()) 
                   noOfDays += 31;
               else 
                   noOfDays += 30;

            countMonth++;
        }                   



    }

    void getDaysThatInYear()
    {
        countYear = 1;

        while(countYear<year)
        {
            if( leapYearCheck(countYear)== true )  
                    noOfDays += 366; 
                else 
                    noOfDays += 365;

            countYear ++;
        }
    }

    void noOfDaysAndDayName()
    {
        System.out.println("The noOfDays is"+noOfDays);

        System.out.println("And Your Day is :"); 

        int day;

        day = (int)(noOfDays%7);
        switch(day)
        {

        case 0:
            System.out.println("Sunday");
            break;
        case 1:
            System.out.println("Monday");
            break;
        case 2:
            System.out.println("Tuesday");
            break; 
        case 3:
            System.out.println("Wednesday");
            break;
        case 4:
            System.out.println("Thursday");
            break;
        case 5:
            System.out.println("Friday");
            break;
        case 6:
            System.out.println("Saturday");
            break;
        }

    }

}// end of MonthToDate class

public class Main
{
    public static void main(String args[])
    {

        DayDisplayer  dd= new DayDisplayer();

        dd.getDaysThatInYear();
        dd.getDaysThatInMonth();
        dd.getDaysThatInDays();

        dd.noOfDaysAndDayName();


    }



}

在这段代码中,当我将 01/01/0001 作为星期一并计算其他日子时。我得到了正确的答案。

但是在www.timeanddate.com 网站上,他们将 01/01/0001 作为星期六。 但是对于最近的其他日期(比如 2011 年 7 月 17 日),他们仍然给出了正确的答案(如星期日)。

我可以猜测这些滞后是由于 Julier 系统迁移到公历系统。

但我怀疑我从 0001 年开始计算天数的方法是否正确?

另外,我对 01/01/0001 日期是星期一还是星期六有疑问。 (如果我把星期六作为我的第一天,我会得到错误的答案。)

请有人解释一下。

提前致谢。

【问题讨论】:

  • 你忘记了作业标签...
  • 您还忘记了您正在使用的编程语言的标签

标签: java calendar julian-date


【解决方案1】:

您总是可以与 Java 本身为您提供的比较:

import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.FieldPosition;

public class Test {

    public static void main (String args[]) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the date please");

        int days = scan.nextInt();
        int month = scan.nextInt();
        int year = scan.nextInt();

        SimpleDateFormat sdf = new SimpleDateFormat("E dd-MM-yyyy G");
        StringBuffer buf = new StringBuffer();
        Calendar cal = new GregorianCalendar();
        cal.set(year, month-1, days);
        sdf.format(cal.getTime(), buf, new FieldPosition(0));
        System.out.println(buf.toString());

    }


}

所以我们得到:

> java Test
Enter the date please
1
1
0001
Sat 01-01-0001 AD

这似乎与网站一致......

【讨论】:

    【解决方案2】:

    您的开篇评论中的一个错误可能会突出显示一个问题:

    The noOfDays variable will count the no of days since 01/01/0001. E.g 
    noOfDays = 365 for 01/12/0001 and noOfDays = 731 for 01/01/0003 and so on.
    

    这表示 0001-01-01 将是第 0 天(自参考日期起 0 天),这反过来意味着您的预测公历上的 0001-12-31 将是 364,而不是 365。

    您可以通过以下两种方式之一进行调整:

    • 将 0001-01-01 定义为第 1 天(因此引用的评论片段的其余部分是正确的)。
    • 将评论值更改为 364 和 730。

    逆向工程的另一个问题可以追溯到那么远的时候,就是理解“预测”的公历。该术语严格表示“事物在实际发生或发生之前就存在的表示”,并向前应用,但该术语也用于历法计算,指的是在时间上向后应用当前规则。手头的问题是公历及其闰年规则(可被 400 整除,或可被 4 整除但不能被 100 整除)在第一年不存在。问题是:您提到的网站是否进行了补偿儒略历和公历之间的转换? (这背后有很多复杂性;即使在公元前 55 年到公元 200 年之间的罗马帝国,日历也异常不稳定。)


    Calendrical Calculations 是一本很好的思考时间问题的书。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-23
      • 2018-12-06
      • 2015-12-31
      • 1970-01-01
      • 2013-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多