3

DateTimesオブジェクトに 2 つ(Joda) を格納してPeriodから、オブジェクトからを取得しますnew Period(dateTime1, dateTime2)。次に、さまざまなオブジェクトのすべての期間を一緒に追加したいと考えています。すべての期間を変数に追加し、HashMap<long, Period>.

その結果と課題はこれです。最初の期間は、「2 時間 30 分」を取得PeriodFormat.getDefault().print(p) します (getHours と getMinutes を連結した場合、値は同じです)。2 番目の値「5 時間 52 分」。ここまでは順調ですね。しかし、3番目と4番目でそれを行うと、分が時間に変換されなくなります。

「5時間103分」

「8時間132分」

10h 12m のはずですが、ご覧のとおりです。それは私が得ているものではありません。どうした?どうすればPeriod変換を忘れることができますか? 選択した金額にまだ問題はありません。

コード: (変数名を変更)

mainSum= new Period();
tasksSum= new HashMap<Long, Period>();
for(Entry entry: entries){
        long main_id= entry.getMain_id();
        long task_id = entry.getTask_id();
        Period entryPeriod = entry.getPeriod();

        if(main_id == mainStuff.getId()){
            mainSum = entryPeriod.plus(mainSum);
            Timber.d("mainSum: " + PeriodFormat.getDefault().print(mainSum));
            Timber.d("sum of workplace: " + mainSum.getHours() + " : " + mainSum.getMinutes());
            Period taskPeriod = tasksPeriodSums.remove(task_id);
            if(taskPeriod == null){
                tasksPeriodSums.put(task_id, entryPeriod);
            } else {
                tasksPeriodSums.put(task_id, taskPeriod.plus(entryPeriod));
            }
        }
    }

助けてください、ありがとう:)

4

1 に答える 1

2

plus(Period)これは文書化された動作です。関数の Javadoc を確認してください。

/**
 * Returns a new period with the specified period added.
 * <p>
 * Each field of the period is added separately. Thus a period of
 * 2 hours 30 minutes plus 3 hours 40 minutes will produce a result
 * of 5 hours 70 minutes - see {@link #normalizedStandard()}.
 * <p>
...

関数自体の Javadoc をドリルダウンするとnormalizedStandard(..)、トレードオフが何であるかがわかります。

/**
 * Normalizes this period using standard rules, assuming a 12 month year,
 * 7 day week, 24 hour day, 60 minute hour and 60 second minute,
 * 
...
 * However to achieve this it makes the assumption that all years are
 * 12 months, all weeks are 7 days, all days are 24 hours,
 * all hours are 60 minutes and all minutes are 60 seconds. This is not
 * true when daylight savings time is considered, and may also not be true
 * for some chronologies.
...
于 2015-05-29T19:53:19.040 に答える