两个本地时间的平均值

Average of two LocalTimes

如何获得两个 LocalTimes 的平均值?找不到任何合适的方法。

因此,例如 08:00 和 14:30,应该 return (14-8)/2 = 3 + 分钟 (30-00= 30)/2,所以 3:15 然后像

Localtime xxx = LocalTime.parse("08:00", formatter).plus(3, ChronoUnit.HOURS); 
//and after that's done
xxx = xxx.plus(15, ChronoUnit.MINUTES);

现在假设我有以下代码:

   //this means that if code is 08:00, it should look whether the average of Strings split21 and split2 (which are put in time2 and time3, where time2 is ALWAYS before time3) is before 08:00
   if(code1.contains("800")) {

        LocalTime time1 = LocalTime.parse("08:00", formatter);
        LocalTime time2 = LocalTime.parse(split21, formatter);
        LocalTime time3 = LocalTime.parse(split2, formatter);
        LocalTime average = 
        if(time2.isBefore(time1)) {
            return true;
        }
        else {
            return false;
        }
    }

显然我可以 use.getHour 和 .getMinute ,但是这里有两个问题。

  1. 我不能划分 LocalTime(只有在分别处理小时和分钟时才有效,但老实说,这有点太中世纪了
  2. 如果我不直接划分小时和分钟,它将高于 24:00 并且我不知道会发生什么:我想 00:00 等会更进一步例如 36:00 而不是

有人能完成这个吗code/explain怎么了?

我想你的意思是这样的:

public static LocalTime average(LocalTime time1, LocalTime time2) {
    if (time1.isAfter(time2)) {
        return LocalTime.of(
                time1.plusHours(time2.getHour()).getHour() / 2,
                time1.plusMinutes(time2.getMinute()).getMinute() / 2
        );
    } else {
        return LocalTime.of(
                time2.plusHours(time1.getHour()).getHour() / 2,
                time2.plusMinutes(time1.getMinute()).getMinute() / 2
        );
    }
}

然后你可以多次调用你的方法:

LocalTime time1 = LocalTime.of(14, 30);
LocalTime time2 = LocalTime.of(8, 00);

LocalTime result = average(time1, time2);

以三次为例:

LocalTime time1 = LocalTime.of(14, 30);
LocalTime time2 = LocalTime.of(8, 00);
LocalTime time3 = LocalTime.now();

LocalTime result = average(average(time1, time2), time3);

..等等

第一个例子的输出

11:15

考虑到您在代码中的评论,我得出了这段代码 //(放在 time2 和 time3 中,其中 time2 总是在 time3 之前)

DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_TIME;

    LocalTime time1 = LocalTime.parse("08:00", formatter);
    LocalTime time2 = LocalTime.parse("14:30", formatter);

    int hour1 = time1.get(ChronoField.CLOCK_HOUR_OF_DAY);
    int hour2 = time2.get(ChronoField.CLOCK_HOUR_OF_DAY);

    int min1 = time1.get(ChronoField.MINUTE_OF_HOUR);
    int min2 = time2.get(ChronoField.MINUTE_OF_HOUR);

    int avgHour = (hour2-hour1)/2;
    int avgMin = (min2-min1)/2;

    String newavgHour = "00:00";
    if(String.valueOf(avgHour).length() == 1) {
        newavgHour = "0"+avgHour+":00";
    } else {
        newavgHour = avgHour+":00";
    }

    LocalTime avgTime = LocalTime.parse(newavgHour, formatter).plus(avgMin, ChronoUnit.MINUTES);
    System.out.println(avgTime);

由于 LocalTime 是由自午夜起的纳秒有效定义的,您可以这样做:

public static LocalTime average(LocalTime t1, LocalTime... others) {
  long nanosSum = t1.toNanoOfDay();
  for (LocalTime other : others) {
    nanoSum += others.toNanoOfDay();
  }
  return LocalTime.ofNanoOfDay(nanoSum / (1+others.length));
}

您可以使用 Java 8 java.time 包,使用 LocalTime.ofSecondOfDay(long) 方法轻松地做到这一点。这实际上是一天中小时和分钟(和秒)的组合。

public static LocalTime average(LocalTime... times) {
    return LocalTime.ofSecondOfDay((long) Arrays.stream(times)
        .mapToInt(LocalTime::toSecondOfDay)
        .average()
        .getAsDouble());
}
LocalTime t1 = LocalTime.of(8, 0);
LocalTime t2 = LocalTime.of(14, 30);
System.out.println(average(t1, t2)); // Prints 11:15

我想你的问题的方式是你想要两次之间的中点。以这种方式思考,我发现最自然的做法是取两次之间的差值,除以 2 并加上第一次(或从第二次减去)。这似乎也是您在问题中尝试过的。不要自己处理小时和分钟,而是使用 Duration class:

    LocalTime time1 = LocalTime.of(8, 0);
    LocalTime time2 = LocalTime.of(14, 30);
    Duration diff = Duration.between(time1, time2);
    LocalTime midpoint = time1.plus(diff.dividedBy(2));
    System.out.println(midpoint);

输出:

11:15

显然只能用两次,不能用三次以上。在其他几个答案中很好地处理了两次以上的情况。