计算24小时时钟的平均时间(hh:mm:ss)

时间:2019-05-24 06:35:51

标签: java

我有时间在HHMMSS 23:00:00,1:00:00。因此,平均时间应为00:00:00。我正在用Java执行此操作。但是我听不懂逻辑

我已经转到此链接 'Find average of time in (hh mm ss) format in java' 表示乘以60,但是如果时间在23:00:00和1:00:00之后,则无法使用。

public static String calculateAverageOfTime(String timeInHHmmss) {
String[] split = timeInHHmmss.split(" ");
long seconds = 0;
for (String timestr : split) {
    String[] hhmmss = timestr.split(":");
    seconds += Integer.valueOf(hhmmss[0]) * 60 * 60;
    seconds += Integer.valueOf(hhmmss[1]) * 60;
    seconds += Integer.valueOf(hhmmss[2]);
}
seconds /= split.length;
long hh = seconds / 60 / 60;
long mm = (seconds / 60) % 60;
long ss = seconds % 60;
return String.format("%02d:%02d:%02d", hh,mm,ss);}

3 个答案:

答案 0 :(得分:2)

考虑到时间是按时间顺序排列的,因此在“ 23:00:00 1:00:00”中1:00:00表示第二天的凌晨1点,您可以使用以下calculateAverageOfTime方法变体

public static String calculateAverageOfTime(String timeInHHmmss) {
String[] split = timeInHHmmss.split(" ");
long seconds = 0;
long lastSeconds = 0;

for (String timestr : split) {
    String[] hhmmss = timestr.split(":");
    long currentSeconds = 0;

    currentSeconds += Integer.valueOf(hhmmss[0]) * 60 * 60;
    currentSeconds += Integer.valueOf(hhmmss[1]) * 60;
    currentSeconds += Integer.valueOf(hhmmss[2]);

    if (currentSeconds < lastSeconds)
        currentSeconds += 24 * 60 * 60; //next day
    seconds += currentSeconds;
    lastSeconds = currentSeconds;

}
seconds /= split.length;

long hh = (seconds / 60 / 60) % 24;
long mm = (seconds / 60) % 60;
long ss = seconds % 60;
return String.format("%02d:%02d:%02d", hh,mm,ss);}

答案 1 :(得分:1)

这是使用LocalTimeDuration的解决方案

static LocalTime stringToTime(String str) {
    String[] components = str.split(":");
    return  LocalTime.of( Integer.valueOf(components[0]), Integer.valueOf(components[1]), Integer.valueOf(components[2]));
}

public static String calculateAverageOfTime(String timeInHHmmss) {
  String[] timesArray = timeInHHmmss.split(" ");

  LocalTime startTime = stringToTime(timesArray[0]);
  LocalTime endTime = stringToTime(timesArray[1]);

  Duration duration = Duration.between(startTime, endTime);
  if (startTime.isAfter(endTime)) {
    duration = duration.plusHours(24);
  }

  LocalTime average = startTime.plus(duration.dividedBy(2L));
  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");

  return average.format(dtf);
}

不存在错误处理,因此我假设输入字符串包含两个格式正确的时间值

答案 2 :(得分:0)

一个更简单的实现如下所示:

LocalDateTime min = LocalDateTime.now().withHour(23).withMinute(0).withSecond(0).withNano(0);
LocalDateTime max = LocalDateTime.now().plusDays(1).withHour(1).withMinute(0).withSecond(0).withNano(0);
LocalDateTime avg = min.plus(ChronoUnit.MILLIS.between(min, max)/2, ChronoUnit.MILLIS);

示例:

LocalDateTime min = LocalDateTime.now().withHour(20).withMinute(0).withSecond(0).withNano(0);
LocalDateTime max = LocalDateTime.now().withHour(22).withMinute(0).withSecond(0).withNano(0);
LocalDateTime avg = min.plus(ChronoUnit.MILLIS.between(min, max)/2, ChronoUnit.MILLIS);
System.out.println(min);
System.out.println(max);
System.out.println(avg);

输出:

2019-05-24T20:00
2019-05-24T22:00
2019-05-24T21:00