「Kotlin」betweenメソッドで日の差分を求めるサンプル

2021年12月6日

書式
public long between(Temporal temporal1Inclusive,
Temporal temporal2Exclusive)
2つの時間的オブジェクトの間の時間量を計算します。
実装では、2番目の型を最初の型のインスタンスに変換してから、量を計算します。終了が開始より前である場合、結果は負になります。
DAYS 1日の概念を表す単位。
2021年11月1日から2022年3月12日の日の差分を求めます。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.time.temporal.ChronoUnit
import java.time.LocalDate
fun main(args: Array<String>) {
// 日付1
val dtA = LocalDate.of(2021, 11, 1)
// 日付2
val dtB = LocalDate.of(2022, 3, 12)
println("日の差分を求める結果")
println( ChronoUnit.DAYS.between(dtA, dtB))
}
import java.time.temporal.ChronoUnit import java.time.LocalDate fun main(args: Array<String>) { // 日付1 val dtA = LocalDate.of(2021, 11, 1) // 日付2 val dtB = LocalDate.of(2022, 3, 12) println("日の差分を求める結果") println( ChronoUnit.DAYS.between(dtA, dtB)) }
import java.time.temporal.ChronoUnit
import java.time.LocalDate

fun main(args: Array<String>) {
    // 日付1
    val dtA = LocalDate.of(2021, 11, 1)

    // 日付2
    val dtB = LocalDate.of(2022, 3, 12)
    println("日の差分を求める結果")
    println( ChronoUnit.DAYS.between(dtA, dtB))
}

実行結果
日の差分を求める結果
131

Kotlin

Posted by arkgame