YearMonth

@Serializable(with = YearMonthSerializer::class)
actual class YearMonth(val year: Int, month: Int) : Comparable<YearMonth> (source)
@Serializable(with = YearMonthSerializer::class)
expect class YearMonth(year: Int, month: Int) : Comparable<YearMonth> (source)

The year-month part of LocalDate, without a day-of-month.

This class represents years and months without a reference to a particular time zone. As such, these objects may denote different time intervals in different time zones: for someone in Berlin, 2020-08 started and ended at different moments from those for someone in Tokyo.

Arithmetic operations

The arithmetic on YearMonth values is defined independently of the time zone (so 2020-08 plus one month is 2020-09 everywhere).

Operations with DateTimeUnit.MonthBased are provided for YearMonth:

Platform specifics

The range of supported years is unspecified, but at least is enough to represent year-months of all instants between Instant.DISTANT_PAST and Instant.DISTANT_FUTURE in any time zone.

On the JVM, there are YearMonth.toJavaYearMonth() and java.time.YearMonth.toKotlinYearMonth() extension functions to convert between kotlinx.datetime and java.time objects used for the same purpose. Similarly, on the Darwin platforms, there is a YearMonth.toNSDateComponents() extension function.

Construction, serialization, and deserialization

YearMonth can be constructed directly from its components using the constructor. See sample 1.

parse and toString methods can be used to obtain a YearMonth from and convert it to a string in the ISO 8601 extended format. See sample 2.

parse and YearMonth.format both support custom formats created with Format or defined in Formats. See sample 3.

Additionally, there are several kotlinx-serialization serializers for YearMonth:

Samples

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

fun main() { 
   //sampleStart 
   // Constructing a YearMonth value using its constructor
val yearMonth = YearMonth(2024, 4)
check(yearMonth.year == 2024)
check(yearMonth.month == Month.APRIL) 
   //sampleEnd
}
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlinx.datetime.onDay
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Parsing and formatting YearMonth values
check(YearMonth.parse("2023-01") == YearMonth(2023, Month.JANUARY))
check(YearMonth(2023, Month.JANUARY).toString() == "2023-01") 
   //sampleEnd
}
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlinx.datetime.onDay
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Parsing and formatting YearMonth values using a custom format
val customFormat = YearMonth.Format {
    monthName(MonthNames.ENGLISH_ABBREVIATED); chars(", "); year()
}
val yearMonth = customFormat.parse("Apr, 2024")
check(yearMonth == YearMonth(2024, Month.APRIL))
val formatted = yearMonth.format(customFormat)
check(formatted == "Apr, 2024") 
   //sampleEnd
}
@Serializable(with = YearMonthSerializer::class)
actual class YearMonth(year: Int, month: Int) : Comparable<YearMonth> , Serializable(source)

Constructors

Link copied to clipboard
actual constructor(year: Int, month: Month)
actual constructor(year: Int, month: Int)
expect constructor(year: Int, month: Month)

Constructs a YearMonth instance from the given year-month components.

expect constructor(year: Int, month: Int)
actual constructor(year: Int, month: Month)
actual constructor(year: Int, month: Int)

Types

Link copied to clipboard
actual object Companion
expect object Companion
actual object Companion
Link copied to clipboard
actual object Formats
expect object Formats

A collection of predefined formats for parsing and formatting YearMonth values.

actual object Formats

Properties

Link copied to clipboard
actual val days: LocalDateRange
expect val days: LocalDateRange

Returns the range of days in the year-month.

actual val days: LocalDateRange
Link copied to clipboard
actual val firstDay: LocalDate
expect val firstDay: LocalDate

Returns the first day of the year-month.

actual val firstDay: LocalDate
Link copied to clipboard
actual val lastDay: LocalDate
expect val lastDay: LocalDate

Returns the last day of the year-month.

actual val lastDay: LocalDate
Link copied to clipboard
actual val month: Month
expect val month: Month

Returns the month (Month) component of the year-month.

actual val month: Month
Link copied to clipboard
actual val numberOfDays: Int
expect val numberOfDays: Int

Returns the number of days in the year-month.

actual val numberOfDays: Int
Link copied to clipboard
actual val year: Int
expect val year: Int

Returns the year component of the year-month.

actual val year: Int

Functions

Link copied to clipboard
actual open operator override fun compareTo(other: YearMonth): Int
expect open operator override fun compareTo(other: YearMonth): Int

Compares this date with the other year-month. Returns zero if this year-month represents the same month as the other (meaning they are equal to one other), a negative number if this year-month is earlier than the other, and a positive number if this year-month is later than the other.

actual open operator override fun compareTo(other: YearMonth): Int
Link copied to clipboard

Creates a YearMonthProgression from this down to that, inclusive.

equals
Link copied to clipboard
open operator override fun equals(other: Any?): Boolean
open operator override fun equals(other: Any?): Boolean
Link copied to clipboard

Formats this value using the given format. Equivalent to calling DateTimeFormat.format on format with this.

hashCode
Link copied to clipboard
open override fun hashCode(): Int
open override fun hashCode(): Int
Link copied to clipboard

Returns a YearMonth that results from subtracting the value number of the specified unit from this year-month.

Link copied to clipboard

The YearMonth one month earlier.

Link copied to clipboard

The YearMonth 12 months earlier.

Link copied to clipboard

Returns the number of whole months between two year-months.

Link copied to clipboard

Combines this year-month with the specified day-of-month into a LocalDate value.

Link copied to clipboard

Returns a YearMonth that results from adding the value number of the specified unit to this year-month.

Link copied to clipboard

The YearMonth one month later.

Link copied to clipboard

The YearMonth 12 months later.

Link copied to clipboard
actual operator fun rangeTo(that: YearMonth): YearMonthRange
expect operator fun rangeTo(that: YearMonth): YearMonthRange

Creates a YearMonthRange from this to that, inclusive.

actual operator fun rangeTo(that: YearMonth): YearMonthRange
Link copied to clipboard
actual operator fun rangeUntil(that: YearMonth): YearMonthRange
expect operator fun rangeUntil(that: YearMonth): YearMonthRange

Creates a YearMonthRange from this to that, exclusive, i.e., from this to (that - 1 month)

actual operator fun rangeUntil(that: YearMonth): YearMonthRange
Link copied to clipboard
Link copied to clipboard
fun YearMonth.toNSDateComponents(): NSDateComponents

Converts the given YearMonth to NSDateComponents.

Link copied to clipboard
actual open override fun toString(): String
expect open override fun toString(): String

Converts this year-month to the extended ISO 8601 string representation.

actual open override fun toString(): String
Link copied to clipboard

Returns the whole number of the specified month-based units between this and other year-months.

Link copied to clipboard

Returns the number of whole years between two year-months.