小时和整数之间的差异

小时和整数之间的差异

本文介绍了小时和整数之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在时间表和Integer(此Integer是小时数)之间进行区别。

I'm looking to do a difference between an schedule and Integer (this Integer is a number of hours).

我希望计算小时数和分钟。我想显示

I'm looking to compute number hours and minutes. I would like display

如何更改代码以计算小时和分钟?

How can I change my code to compute hours and minutes ?

推荐答案

内部带有注释的可能方法

Possible approach with comments inside

import java.time.temporal.ChronoUnit
import java.time.LocalTime

import scala.concurrent.duration._

val t = LocalTime.now()


def toEnd(t: LocalTime) = {
  // start of the day
  val start = LocalTime.of(9, 0)
  // end of first half
  val midEnd = LocalTime.of(13, 0)
  // start of second half
  val midStart = LocalTime.of(14, 0)
  // end of the day
  val end = LocalTime.of(18, 0)


  // before start of the day
  if (t.isBefore(start)) 8.hours
  // first half
  else if (t.isBefore(midEnd)) t.until(midEnd, ChronoUnit.MILLIS).millis + 4.hours
  // in between
  else if (t.isBefore(midStart)) 4.hours
  // second half
  else if (t.isBefore(end)) t.until(end, ChronoUnit.MILLIS).millis
  // after the end
  else 0.hours
}

// here you can add any specific format for evaluated duration
implicit class formatter(d: FiniteDuration) {
  def withMinutes = {
    // convert to minutes
    val l = d.toMinutes
    // format
    s"${l / 60}:${l % 60}"
  }
}

toEnd(t).withMinutes
toEnd(LocalTime.of(9, 30)).withMinutes
toEnd(LocalTime.of(12, 30)).withMinutes
toEnd(LocalTime.of(13, 30)).withMinutes
toEnd(LocalTime.of(14, 30)).withMinutes

这篇关于小时和整数之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:11