我希望在计划和整数之间做一个区别(这个整数是几个小时)。
我想要计算小时数和分钟数。我想显示
如何更改代码以计算小时和分钟?
答案 0 :(得分:1)
内部评论的可能方法
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