1

いくつかのフォーラムを調べた後、私は日数の違いを見つけるために以下のコードを使用することになりました。しかし、ロジックに問題があります(私の見落としかもしれません)。11から14と11から15の日数の差は同じであることがわかります。どうしてそれは可能ですか?

Date createdDate = new Date((2013 + 1900), (1 + 1), 11);
Date expirationDate = new Date((2013 + 1900), (1 + 1), 11);
for (int i = 11; i < 20; i++) {
    expirationDate.setDate(i);

    System.out.println("11 to " + i + " = "
            + (int) (expirationDate.getTime() - createdDate.getTime())
            / (1000 * 60 * 60 * 24));
}

出力は次のとおりです。

11 to 11 = 0
11 to 12 = 1
11 to 13 = 2
11 to 14 = 3
11 to 15 = 3
11 to 16 = 4
11 to 17 = 5
11 to 18 = 6
11 to 19 = 7
4

5 に答える 5

4

Use Joda Time's Days#daysBetween(). There is no better way.

DateMidnight createdDate = new DateMidnight(2013, 2, 11);

for (int i = 11; i < 20; i++) {

    DateMidnight expirationDate = new DateMidnight(2013, 2, i);
    int dayDifference = Days.daysBetween(createdDate, expirationDate);

    System.out.println("11 to " + i + " = " + dayDifference);
}
于 2013-01-11T00:11:56.050 に答える
1

Date(year、day、month)コンストラクターは非推奨になりました。2日間の差を取得するには、Calendarsメソッドを使用するだけです。

Calendar cal1=Calendar.getInstance();
Calendar cal2=Calendar.getInstance();
cal1.setTime(createdDate);
cal2.setTime(expirationDate);
System.out.println(cal2.get(Calendar.DAY_OF_MONTH )-cal1.get(Calendar.DAY_OF_MONTH ) );

編集:

Calendar cal1 = Calendar.getInstance();
cal1.set(2013, 2, 11);
Calendar cal2 = Calendar.getInstance();
cal2.set(2013, 2, 11);
for (int i = 11; i < 20; i++) {
 cal2.set(Calendar.DATE, i);
    System.out.println("11 to " + i + " = " + (cal2.get(Calendar.DAY_OF_MONTH) -cal1.get(Calendar.DAY_OF_MONTH)));
}

出力:

11 to 11 = 0
11 to 12 = 1
11 to 13 = 2
11 to 14 = 3
11 to 15 = 4
11 to 16 = 5
11 to 17 = 6
11 to 18 = 7
11 to 19 = 8
于 2013-01-11T00:04:24.463 に答える
1

1000 * 60 * 60 * 24日差を見つける間違った方法です。JodaTimeを使用できますが、純粋なJavaソリューションがあります。

2つの初期化された変数があるとします

Calendar firstDay = ...;
Calendar secondDay = ...;

firstDay.before(lastDay)

ですtrue

走る

int firstDayNo = firstDay.get(Calendar.DAY_OF_YEAR);
int secondDayNo = secondDay.get(Calendar.DAY_OF_YEAR);
int dayDifference, yearMultiplier;

dayDifference = -firstDayNo;
yearMultiplier = secondDay.get(Calendar.YEAR) - firstDay.get(Calendar.YEAR);
while (yearMultiplier > 0) {
    dayDifference += firstDay.getActualMaximum(Calendar.DAY_OF_YEAR);
    firstDay.add(Calendar.YEAR, 1);
    yearMultiplier--;
}
dayDifference += secondDayNo;

return dayDifference;
于 2015-01-13T17:03:23.260 に答える
0

floatを使用すると、問題が発生します。タイムスタンプを使用することは、2つの日付間の日の違いを見つけるための良いアプローチのようには思えません。

11〜11 = 0.0
11〜12 = 1.0 11〜13
= 2.0 11〜14
= 3.0
11〜15
= 3.9583333 11〜16
= 4.9583335 11〜17
= 5.9583335 11〜18
= 6.9583335 11〜19 = 7.9583335

将来的には、日付の違いを決定するための最も決定的な方法を次のように見つけます。

Calendar cre_calendar = new GregorianCalendar((2013), (1), 11);
        Calendar exp_calendar = new GregorianCalendar((2013), (1), 19);
        Calendar maxDays = new GregorianCalendar(((2013)), (12), 31);

        if (exp_calendar.get(Calendar.DAY_OF_YEAR) < cre_calendar
                .get(Calendar.DAY_OF_YEAR)) {
            System.out
                    .println((exp_calendar.get(Calendar.DAY_OF_YEAR) + maxDays
                            .get(Calendar.DAY_OF_YEAR))
                            - cre_calendar.get(Calendar.DAY_OF_YEAR));
        } else {
            System.out.println((exp_calendar.get(Calendar.DAY_OF_YEAR))
                    - cre_calendar.get(Calendar.DAY_OF_YEAR));
        }
于 2013-01-11T00:20:19.793 に答える
0

少なくとも現在のバージョンでは、コードは正しい結果を出力します

11 to 11 = 0
11 to 12 = 1
11 to 13 = 2
11 to 14 = 3
11 to 15 = 4
11 to 16 = 5
11 to 17 = 6
11 to 18 = 7
11 to 19 = 8

それnew Date((2013 + 1900), (1 + 1), 11);にもかかわらず、正しくありません、実際にはそうです5813-03-01new Date((2013 - 1900), (1 - 1), 11);Date(int year、int month、int day)APIが表示されます。

Parameters:
year - the year minus 1900.
month - the month between 0-11.
date - the day of the month between 1-31.
于 2013-01-11T01:48:16.533 に答える