Java 使用Joda Time获得平均时间
我使用一个Joda时间段对多个时间值求和。从00:00:00开始,我将每个时间量相加,以获得所有活动的总时间量。我想通过求和所有时间值得到的总时间除以总距离,得到每个测量单位的平均时间 代码: 输出:Java 使用Joda Time获得平均时间,java,time,distance,jodatime,average,Java,Time,Distance,Jodatime,Average,我使用一个Joda时间段对多个时间值求和。从00:00:00开始,我将每个时间量相加,以获得所有活动的总时间量。我想通过求和所有时间值得到的总时间除以总距离,得到每个测量单位的平均时间 代码: 输出: Orig Time: 00:00:00 Run - 00:16:08 - 2.0 miles Adding Time: 00:16:08 Altered Time: 00:16:08 Run - 00:18:09 - 2.21 miles Add
Orig Time: 00:00:00
Run - 00:16:08 - 2.0 miles
Adding Time: 00:16:08
Altered Time: 00:16:08
Run - 00:18:09 - 2.21 miles
Adding Time: 00:34:17
Altered Time: 00:34:17
Run - 00:23:35 - 2.69 miles
Adding Time: 00:57:52
Altered Time: 00:57:52
Run - 00:34:45 - 4.29 miles
Adding Time: 00:91:97
Altered Time: 01:32:37
Run - 00:36:15 - 4.17 miles
Adding Time: 01:68:52
Altered Time: 02:08:52
Run - 00:18:03 - 2.14 miles
Adding Time: 02:26:55
Altered Time: 02:26:55
Run - 00:17:27 - 2.19 miles
Adding Time: 02:43:82
Altered Time: 02:44:22
Run - 00:43:43 - 5.1 miles
Adding Time: 02:87:65
Altered Time: 03:28:05
Distance: 24.79
Final Time: 03:28:05
将最终时间03:28:05除以24.79得到单位平均时间的最佳方法是什么?这里是我最后的答案:
PeriodFormatterBuilder builder = new PeriodFormatterBuilder();
PeriodFormatter formatter =
builder.printZeroAlways()
.minimumPrintedDigits(2)
.appendHours()
.appendSeparator(":")
.appendMinutes()
.appendSeparator(":")
.appendSeconds()
.toFormatter();
String zero = "00:00:00";
Period period = formatter.parsePeriod(zero);
float total = 0;
for (Activity temp : last7acts) {
System.out.println(
temp.getCatName() + " - " + temp.getTime()
+ " - " + temp.getDistance() + " " + temp.getUnits());
total += temp.getDistance();
period = period.plus(formatter.parsePeriod(temp.getTime()));
}
period = period.normalizedStandard(PeriodType.dayTime().withDaysRemoved());
System.out.println("Final Distance: " + total);
System.out.println("Final Time: " + formatter.print(period));
long secs = period.toStandardDuration().getStandardSeconds();
NumberFormat nf = NumberFormat.getInstance(Locale.getDefault());
System.out.println(nf.format(secs / total) + " seconds/mile");
long minutes = secs / 60;
System.out.println(nf.format(minutes / total) + " minutes/mile");
看看这个循环有多短!不可变的
Period
类型通常是更好的选择,它提供了规范化功能,您需要并尝试以冗长的方式手动执行这些功能。您的变量start是什么意思?哪种类型?将时间转换为秒除以你的英里数,你得到的是每英里多少秒?感谢gfgaxiola,这是一种选择,但很好奇是否有功能可以在不转换的情况下按时执行除法。显然不是。Meno-开始变量是可变周期类型。我用“00:00:00”时间初始化它,并用它将时间相加。
PeriodFormatterBuilder builder = new PeriodFormatterBuilder();
PeriodFormatter formatter =
builder.printZeroAlways()
.minimumPrintedDigits(2)
.appendHours()
.appendSeparator(":")
.appendMinutes()
.appendSeparator(":")
.appendSeconds()
.toFormatter();
String zero = "00:00:00";
Period period = formatter.parsePeriod(zero);
float total = 0;
for (Activity temp : last7acts) {
System.out.println(
temp.getCatName() + " - " + temp.getTime()
+ " - " + temp.getDistance() + " " + temp.getUnits());
total += temp.getDistance();
period = period.plus(formatter.parsePeriod(temp.getTime()));
}
period = period.normalizedStandard(PeriodType.dayTime().withDaysRemoved());
System.out.println("Final Distance: " + total);
System.out.println("Final Time: " + formatter.print(period));
long secs = period.toStandardDuration().getStandardSeconds();
NumberFormat nf = NumberFormat.getInstance(Locale.getDefault());
System.out.println(nf.format(secs / total) + " seconds/mile");
long minutes = secs / 60;
System.out.println(nf.format(minutes / total) + " minutes/mile");