Month Names
A description of how month names are formatted.
Instances of this class are typically used as arguments to DateTimeFormatBuilder.WithYearMonth.monthName.
Predefined instances are available as ENGLISH_FULL and ENGLISH_ABBREVIATED. You can also create custom instances using the constructor.
An IllegalArgumentException will be thrown if some month name is empty or there are duplicate names.
Samples
import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.YearMonth
import kotlinx.datetime.format.DayOfWeekNames
import kotlinx.datetime.format.MonthNames
import kotlinx.datetime.format.Padding
import kotlinx.datetime.format.alternativeParsing
import kotlinx.datetime.format.char
import kotlin.test.Test
fun main() {
//sampleStart
// Using strings for month names in a custom format
val format = LocalDate.Format {
monthName(MonthNames.ENGLISH_ABBREVIATED) // "Jan", "Feb", ...
char(' ')
dayOfMonth()
chars(", ")
year()
}
check(format.format(LocalDate(2021, 1, 13)) == "Jan 13, 2021")
//sampleEnd
}
import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.YearMonth
import kotlinx.datetime.format.DayOfWeekNames
import kotlinx.datetime.format.MonthNames
import kotlinx.datetime.format.Padding
import kotlinx.datetime.format.alternativeParsing
import kotlinx.datetime.format.char
import kotlin.test.Test
fun main() {
//sampleStart
// Constructing a custom set of month names for parsing and formatting
val germanMonthNames = listOf(
"Januar", "Februar", "März", "April", "Mai", "Juni",
"Juli", "August", "September", "Oktober", "November", "Dezember"
)
// constructing by passing a list of 12 strings
val myMonthNamesFromList = MonthNames(germanMonthNames)
check(myMonthNamesFromList.names == germanMonthNames)
//sampleEnd
}
Constructors
Link copied to clipboard