【问题标题】:Java code to find difference between two time,where time is stored in date type and time is in 24 hour format [duplicate]Java代码查找两个时间之间的差异,其中时间以日期类型存储,时间以24小时格式存储[重复]
【发布时间】:2020-01-29 13:00:50
【问题描述】:

我需要 Java 代码以 24 小时格式查找两次之间的差异 例如: 20:00:00 - 04:00:00

预期输出为 8 小时 但现在输出是 16 小时 当我尝试 12 小时格式时,输出即将到来 4.

下面是用于解析和查找差异的代码

SimpleDateFormat readFormat = new SimpleDateFormat("HH:mm");
Date d1 = readFormat.parse(txtshiftIn);
Date d2 = readFormat.parse(txtshiftOut);
long diff = d2.getTime() - d1.getTime();

输入只是 20:00 和 04:00 没有秒和 AM/PM 部分。

【问题讨论】:

  • @Govinda Sakhare String txtshiftIn=request.getParameter("shiftIn");
  • 所以时间延长了两个不同的日子,它不是同一天的一部分?
  • 是的,时间延长两天,它不是同一天的一部分
  • 我建议你不要使用SimpleDateFormatDate。这些类设计不良且过时,尤其是前者,尤其是出了名的麻烦。而是使用来自java.time, the modern Java date and time APILocalTimeDateTimeFormatter

标签: java time datetime-format


【解决方案1】:

使用Date 的问题是它仍然需要一个实际日期,即使您只使用它的时间部分,所以如果您只是发送它,时间将不正确。

改为使用LocalTimeChronoUnit.HOURS 来获取时间之间的差异。

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm");
LocalTime lt1 = LocalTime.parse(txtshiftIn, dtf);
LocalTime lt2 = LocalTime.parse(txtshiftOut, dtf);

long diff = ChronoUnit.HOURS.between(lt1, lt2);

这将显示-16,这意味着lt1 领先于lt2,这表明lt2 是第二天,因此我们可以对其进行修改以获得差异

if (diff < 0) {
  diff += 24;
}

这将为您提供您所期望的8 小时差。

更新 要计算分钟差异,您可以执行以下操作:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm");
LocalTime lt1 = LocalTime.parse(txtshiftIn, dtf);
LocalTime lt2 = LocalTime.parse(txtshiftOut, dtf);

long diff = ChronoUnit.MINUTES.between(lt1, lt2); //get diff in minutes

if (lt2.isBefore(lt1)) {
  diff += TimeUnit.DAYS.toMinutes(1); //add a day to account for day diff
}

long hours = diff / 60;
long minutes = diff % 60;

LocalTime newTime = LocalTime.parse(String.format("%02d:%02d", hours, minutes), dtf); //Format the difference to be converted to LocalTime

System.out.println(newTime);

这将以分钟为单位产生差异:

08:30

更新 2

这是一个更简洁的方法,它返回 Duration

public Duration timeDifference(String txtshiftIn, String txtshiftOut) {
  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm");
  LocalTime lt1 = LocalTime.parse(txtshiftIn, dtf);
  LocalTime lt2 = LocalTime.parse(txtshiftOut, dtf);

  Duration between = Duration.between(lt1, lt2);

  if (lt2.isBefore(lt1)) { //account for lt2 being on the next day
    between = Duration.ofMinutes(TimeUnit.DAYS.toMinutes(1)).plus(between);
  }

  return between;
}

【讨论】:

  • 预期输出是8,但上面代码的输出是-16
  • 固定答案以适应天数的差异。
  • 太棒了。您可以使用lt2.isBefore(lt1) 来提高可读性
  • 您甚至可以在没有格式化程序的情况下进行解析,只需 LocalTime.parse(txtshiftIn) 和移出时间相同。
  • 但是当我执行代码以查找 20:00 到 4:30 之间的时间时,它给出了 9 小时,预期输出为 8 小时 30 分钟
【解决方案2】:

虽然强烈建议您使用更新的 Java 时间库,例如 LocalTime,但到目前为止,您的逻辑实际上是正确的,但有一点需要注意:

SimpleDateFormat readFormat = new SimpleDateFormat("HH:mm");
Date d1 = readFormat.parse(txtshiftIn);
Date d2 = readFormat.parse(txtshiftOut);
long diff = d2.getTime() - d1.getTime();

在您的代码中,如果 d1 在 d2 之后,您将得到 long diff 的否定结果

所以当你说

"但是现在输出是 16 小时"

实际输出是-16 hrs

当然,减去 16 小时在您的情况下没有多大意义,但是您可以通过简单的技巧轻松解决这个问题,即在 diff 的结果为负数的情况下仅添加 24 小时。 (结果是 -16+24 是您预期的 8)。

所以只需在您发布的代码末尾添加以下行

if(diff < 0) {
    diff = 86400000 + diff;
} 

你会得到你期望的结果! (86400000是24h,以毫秒为单位)

【讨论】:

    猜你喜欢
    • 2013-08-31
    • 2018-11-18
    • 2015-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    相关资源
    最近更新 更多