3

我将两个DateTimes(Joda)存储在一个对象中,然后Period通过new Period(dateTime1, dateTime2). 然后我想将来自不同对象的所有句点加在一起。我既将所有周期添加到一个变量中,又将一些周期总结为存储在HashMap<long, Period>.

结果和问题是这样的。第一个周期得到“2 小时 30 分钟” PeriodFormat.getDefault().print(p) (如果我连接 getHours 和 getMinutes,值是相同的)。第二个值“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>
...

深入研究normalizedStandard(..)函数本身的 Javadoc,我们看到了权衡:

/**
 * 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 回答