0

目前我有一个函数可以获取一天的开始时间和结束时间,并计算两者之间的差异,给我一天的工作时间。我想做的是能够获得 7 天的工作时间,并返回总计,同时保持显示格式(HH:mm)。

我一天的总功能:

Period p = new Period(this.startTime[dayIndex], this.endTime[dayIndex]);
long hours = p.getHours();
long minutes = p.getMinutes();

String format = String.format("%%0%dd", 2);//Ensures that the minutes will always display as two digits.

return Long.toString(hours)+":"+String.format(format, minutes);

this.startTime[] 和 this.endTime[] 都是 DateTime 对象的数组。

有什么建议么?

4

3 回答 3

1

你需要一些东西来保存一周的时间,并且每天调用一次你的函数。

但这意味着您需要重构,以便您的计算器方法不会格式化为字符串,而是返回一个数值,以便您可以轻松地将它们加在一起。

于 2010-01-21T19:35:07.857 回答
1

另一个简单的解决方案:

这是一种单独接收小时和分钟的方法。
参数是:

  • 开始时间
  • 开始分钟
  • 结束时间
  • 结束分钟

首先,分别计算小时和分钟之间的差异:

int hours   = pEndHour - pStartHour;
int minutes = ((60 - pStartMinutes) + pEndMinutes) - 60;

然后,验证“分钟”变量的值是否为负:

// If so, the "negative" value of minutes is our remnant to the next hour
 if (minutes < 0) {
  hours--;
  minutes = 60 + minutes ;
 }

最后,您可以以小时格式打印时间段:

String format = String.format("%%0%dd", 2);
System.out.println( "*** " + hours + " : " + minutes);

就这样。

于 2011-05-26T19:43:52.170 回答
0

我为那些感兴趣的人结束了解决方案

    Period[] p=new Period[7];
    long hours = 0;
    long minutes =0;
    for(int x=0; x<=this.daysEntered;x++)
    {
        p[x] = new Period(this.startTime[x], this.endTime[x]);
        hours += p[x].getHours();
        minutes += p[x].getMinutes();
    }

    hours += minutes/60;
    minutes=minutes%60;

    String format = String.format("%%0%dd", 2);

    return Long.toString(hours)+":"+String.format(format, minutes);
于 2010-06-14T17:36:15.887 回答