【问题标题】:Joda Period not converting all minutes to hours "8h, 132m"Joda Period 没有将所有分钟转换为小时“8h,132m”
【发布时间】:2015-08-12 19:07:03
【问题描述】:

我将两个DateTimes (Joda) 存储在一个对象中,然后我从new Period(dateTime1, dateTime2) 的对象中得到一个Period。 然后我想将来自不同对象的所有句点加在一起。 我在一个变量中将所有周期加在一起,并在存储在HashMap<long, Period> 中的较小周期中总结一些周期。

结果和问题是这样的。 第一个时间段使用PeriodFormat.getDefault().print(p) 获得“2 小时30 分钟”(如果我连接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));
            }
        }
    }

请帮忙,谢谢:)

【问题讨论】:

  • 我通过在plus(p) 函数之后添加.normalizedStandard() 来显示正确的信息。所以它有效,但为什么这是必要的?

标签: java android jodatime period minute


【解决方案1】:

这是记录在案的行为,请查看 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.
...

【讨论】:

  • "期间每个字段单独添加"!谢谢你,巴伦德
猜你喜欢
  • 2014-09-06
  • 1970-01-01
  • 1970-01-01
  • 2015-05-18
  • 2018-12-26
  • 2014-12-01
  • 2015-07-20
  • 1970-01-01
  • 2011-02-14
相关资源
最近更新 更多