gpt4 book ai didi

java - 使用 Joda Time 获取平均时间

转载 作者:行者123 更新时间:2023-11-29 08:54:41 25 4
gpt4 key购买 nike

我正在使用 Joda 时间段对多个时间值求和。从 00:00:00 开始,我将每个时间量相加以获得所有 Activity 的总时间量。我想将所有时间值相加得到的总时间除以总距离,得到每个测量单位的平均时间。

代码:

    builder = new PeriodFormatterBuilder();
builder.printZeroAlways()
.minimumPrintedDigits(2)
.appendHours()
.appendSeparator(":")
.appendMinutes()
.appendSeparator(":")
.appendSeconds();

PeriodFormatter formatter = builder.toFormatter();
PeriodParser parser = builder.toParser();

String zero = "00:00:00";
MutablePeriod start = new MutablePeriod();
MutablePeriod adding = new MutablePeriod();
parser.parseInto(start, zero, 0, Locale.getDefault());

System.out.println("Orig Time: "+formatter.print(start));

float total = 0;

for (Activity temp : last7acts) {
System.out.println(temp.getCatName()+" - "+temp.getTime()+" - "+temp.getDistance()+" "+temp.getUnits());
total = total + temp.getDistance();
String add = temp.getTime();
parser.parseInto(adding, add, 0, Locale.getDefault());
start.add(adding);
System.out.println("Adding Time: "+formatter.print(start));
if (start.getSeconds() > 60) {

int origSeconds = start.getSeconds();
int newMinutes = origSeconds / 60;
int newSeconds = origSeconds % 60;

start.addMinutes(newMinutes);
start.setSeconds(newSeconds);

}

if (start.getMinutes() > 60) {

int origMinutes = start.getMinutes();
int newHours = origMinutes / 60;
int newMinutes = origMinutes % 60;

start.addHours(newHours);
start.setMinutes(newMinutes);
}
System.out.println("Altered Time: "+formatter.print(start));

}

System.out.println("Final Distance: "+total);
System.out.println("Final Time: "+formatter.print(start));

输出:

    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 类型通常是更好的选择,它提供了您需要并试图以冗长的方式手动完成的规范化功能。

关于java - 使用 Joda Time 获取平均时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20891819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com