4

指定された日付の日を表示するプログラムを作成しています。(例: 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の Web サイトでは、0001 年 1 月 1 日が土曜日でした。しかし、他の最近の日付 (たとえば 2011 年 7 月 17 日) については、正しい答え (日曜日など) が返されています。

これらのラグは、ジュリア システムからグレゴリオ システムへの移行によるものと推測できます。

しかし、0001 年からの日数を計算する私のアプローチが正しいかどうかは疑問です。

また、01/01/0001 の日付を月曜日と土曜日のどちらにするかについても疑問があります。(土曜日を初日としたら、間違った答えが返ってきます。)

誰か説明してください。

前もって感謝します。

4

2 に答える 2

1

1 つの問題は、最初のコメントのオフバイワン エラーによって強調表示される可能性があります。

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 が 365 ではなく 364 になることを意味します。

これは、次の 2 つの方法のいずれかで調整できます。

  • 0001-01-01 が 1 日目となるように定義します (その後、引用されたコメントの残りの部分は正しいものになります)。
  • コメント値を 364 と 730 に変更します。

リバース エンジニアリングのもう 1 つの問題は、「先発的」グレゴリオ暦を理解することです。この用語は、厳密には「実際に存在する前、または存在する前に存在するものの表現」を意味し、前方に適用されますが、この用語は暦計算でも使用され、現在のルールを過去に適用することを指します. 当面の問題は、閏年の規則 (400 で割り切れる、または 4 で割り切れるが 100 で割り切れない) を持つグレゴリオ暦が 1 年目に存在しなかったことです。ユリウス暦とグレゴリオ暦の切り替えは?(その根底にあるのは多くの複雑さです。ローマ帝国でさえ、紀元前 55 年頃から紀元 200 年頃まで、暦は非常に不安定でした。)


日付の問題を検討するための優れた本はCalendrical Calculationsです。

于 2011-07-17T14:16:53.437 に答える
0

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

これはウェブサイトと一致しているようです...

于 2011-07-17T08:52:07.327 に答える