Spec-Zone.ru › Kotlin 2

hours

kotlin-stdlib/kotlin.time/Duration/Companion/hours

Начиная с Kotlin: 1.6

val Int.hours: Duration

Возвращает значение типа Duration, равное этому количеству часов типа Int.

Примеры

import kotlin.test.*
import kotlin.time.*
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.microseconds
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds

fun main() { 
   //sampleStart 
   println(26.hours) // 1d 2h
println(0.5.hours) // 30m

// Large values can be converted to an infinite duration
println(10_000_000_000_000.hours) // Infinity

// Double.NaN.hours // will fail with IllegalArgumentException 
   //sampleEnd
}

val Long.hours: Duration

Возвращает значение типа Duration, равное этому количеству часов типа Long.

Примеры

import kotlin.test.*
import kotlin.time.*
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.microseconds
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds

fun main() { 
   //sampleStart 
   println(26.hours) // 1d 2h
println(0.5.hours) // 30m

// Large values can be converted to an infinite duration
println(10_000_000_000_000.hours) // Infinity

// Double.NaN.hours // will fail with IllegalArgumentException 
   //sampleEnd
}

val Double.hours: Duration

Возвращает значение типа Duration, равное этому количеству часов типа Double.

В зависимости от величины значение округляется до целого числа наносекунд или миллисекунд.

Выбрасывает

IllegalArgumentException

если это значение типа Double — NaN.

Примеры

import kotlin.test.*
import kotlin.time.*
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.microseconds
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds

fun main() { 
   //sampleStart 
   println(26.hours) // 1d 2h
println(0.5.hours) // 30m

// Large values can be converted to an infinite duration
println(10_000_000_000_000.hours) // Infinity

// Double.NaN.hours // will fail with IllegalArgumentException 
   //sampleEnd
}

© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.time/-duration/-companion/hours.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API