year

abstract fun year(padding: Padding = Padding.ZERO)(source)

A year number.

By default, for years -9999..9999, it's formatted as a decimal number, zero-padded to four digits, though this padding can be disabled or changed to space padding by passing padding. For years outside this range, it's formatted as a decimal number with a leading sign, so the year 12345 is formatted as "+12345".

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Using the year number in a custom format
val format = LocalDate.Format {
    year(); char(' '); monthNumber(); char('/'); dayOfMonth()
}
check(format.format(LocalDate(2021, 1, 13)) == "2021 01/13")
check(format.format(LocalDate(13, 1, 13)) == "0013 01/13")
check(format.format(LocalDate(-2021, 1, 13)) == "-2021 01/13")
check(format.format(LocalDate(12021, 1, 13)) == "+12021 01/13") 
   //sampleEnd
}