55
66package kotlinx.datetime
77
8+ import kotlinx.datetime.format.*
89import kotlinx.datetime.serializers.LocalDateIso8601Serializer
910import kotlinx.serialization.Serializable
1011
@@ -23,14 +24,18 @@ import kotlinx.serialization.Serializable
2324public expect class LocalDate : Comparable <LocalDate > {
2425 public companion object {
2526 /* *
26- * Parses a string that represents a date in ISO-8601 format
27- * and returns the parsed [LocalDate] value.
27+ * A shortcut for calling [DateTimeFormat.parse].
2828 *
29- * An example of a local date in ISO-8601 format: `2020-08-30`.
29+ * Parses a string that represents a date and returns the parsed [LocalDate] value.
30+ *
31+ * If [format] is not specified, [Formats.ISO] is used.
3032 *
3133 * @throws IllegalArgumentException if the text cannot be parsed or the boundaries of [LocalDate] are exceeded.
34+ *
35+ * @see LocalDate.toString for formatting using the default format.
36+ * @see LocalDate.format for formatting using a custom format.
3237 */
33- public fun parse (isoString : String ): LocalDate
38+ public fun parse (input : CharSequence , format : DateTimeFormat < LocalDate > = getIsoDateFormat() ): LocalDate
3439
3540 /* *
3641 * Returns a [LocalDate] that is [epochDays] number of days from the epoch day `1970-01-01`.
@@ -41,10 +46,67 @@ public expect class LocalDate : Comparable<LocalDate> {
4146 */
4247 public fun fromEpochDays (epochDays : Int ): LocalDate
4348
49+ /* *
50+ * Creates a new format for parsing and formatting [LocalDate] values.
51+ *
52+ * Example:
53+ * ```
54+ * // 2020 Jan 05
55+ * LocalDate.Format {
56+ * year()
57+ * char(' ')
58+ * monthName(MonthNames.ENGLISH_ABBREVIATED)
59+ * char(' ')
60+ * dayOfMonth()
61+ * }
62+ * ```
63+ *
64+ * Only parsing and formatting of well-formed values is supported. If the input does not fit the boundaries
65+ * (for example, [dayOfMonth] is 31 for February), consider using [DateTimeComponents.Format] instead.
66+ *
67+ * There is a collection of predefined formats in [LocalDate.Formats].
68+ */
69+ @Suppress(" FunctionName" )
70+ public fun Format (block : DateTimeFormatBuilder .WithDate .() -> Unit ): DateTimeFormat <LocalDate >
71+
4472 internal val MIN : LocalDate
4573 internal val MAX : LocalDate
4674 }
4775
76+ /* *
77+ * A collection of predefined formats for parsing and formatting [LocalDate] values.
78+ *
79+ * See [LocalDate.Formats.ISO] and [LocalDate.Formats.ISO_BASIC] for popular predefined formats.
80+ * [LocalDate.parse] and [LocalDate.toString] can be used as convenient shortcuts for the
81+ * [LocalDate.Formats.ISO] format.
82+ *
83+ * If predefined formats are not sufficient, use [LocalDate.Format] to create a custom
84+ * [kotlinx.datetime.format.DateTimeFormat] for [LocalDate] values.
85+ */
86+ public object Formats {
87+ /* *
88+ * ISO 8601 extended format, which is the format used by [LocalDate.toString] and [LocalDate.parse].
89+ *
90+ * Examples of dates in ISO 8601 format:
91+ * - `2020-08-30`
92+ * - `+12020-08-30`
93+ * - `0000-08-30`
94+ * - `-0001-08-30`
95+ */
96+ public val ISO : DateTimeFormat <LocalDate >
97+
98+ /* *
99+ * ISO 8601 basic format.
100+ *
101+ * Examples of dates in ISO 8601 basic format:
102+ * - `20200830`
103+ * - `+120200830`
104+ * - `00000830`
105+ * - `-00010830`
106+ */
107+ public val ISO_BASIC : DateTimeFormat <LocalDate >
108+ }
109+
48110 /* *
49111 * Constructs a [LocalDate] instance from the given date components.
50112 *
@@ -77,14 +139,19 @@ public expect class LocalDate : Comparable<LocalDate> {
77139
78140 /* * Returns the year component of the date. */
79141 public val year: Int
142+
80143 /* * Returns the number-of-month (1..12) component of the date. */
81144 public val monthNumber: Int
145+
82146 /* * Returns the month ([Month]) component of the date. */
83147 public val month: Month
148+
84149 /* * Returns the day-of-month component of the date. */
85150 public val dayOfMonth: Int
151+
86152 /* * Returns the day-of-week component of the date. */
87153 public val dayOfWeek: DayOfWeek
154+
88155 /* * Returns the day-of-year component of the date. */
89156 public val dayOfYear: Int
90157
@@ -106,13 +173,21 @@ public expect class LocalDate : Comparable<LocalDate> {
106173 public override fun compareTo (other : LocalDate ): Int
107174
108175 /* *
109- * Converts this date to the ISO-8601 string representation.
176+ * Converts this date to the extended ISO-8601 string representation.
110177 *
111- * @see LocalDate.parse
178+ * @see Formats.ISO for the format details.
179+ * @see parse for the dual operation: obtaining [LocalDate] from a string.
180+ * @see LocalDate.format for formatting using a custom format.
112181 */
113182 public override fun toString (): String
114183}
115184
185+ /* *
186+ * Formats this value using the given [format].
187+ * Equivalent to calling [DateTimeFormat.format] on [format] with `this`.
188+ */
189+ public fun LocalDate.format (format : DateTimeFormat <LocalDate >): String = format.format(this )
190+
116191/* *
117192 * @suppress
118193 */
@@ -159,9 +234,8 @@ public operator fun LocalDate.minus(period: DatePeriod): LocalDate =
159234 if (period.days != Int .MIN_VALUE && period.months != Int .MIN_VALUE ) {
160235 plus(with (period) { DatePeriod (- years, - months, - days) })
161236 } else {
162- minus(period.years, DateTimeUnit .YEAR ).
163- minus(period.months, DateTimeUnit .MONTH ).
164- minus(period.days, DateTimeUnit .DAY )
237+ minus(period.years, DateTimeUnit .YEAR ).minus(period.months, DateTimeUnit .MONTH )
238+ .minus(period.days, DateTimeUnit .DAY )
165239 }
166240
167241/* *
@@ -299,3 +373,6 @@ public expect fun LocalDate.plus(value: Long, unit: DateTimeUnit.DateBased): Loc
299373 * @throws DateTimeArithmeticException if the result exceeds the boundaries of [LocalDate].
300374 */
301375public fun LocalDate.minus (value : Long , unit : DateTimeUnit .DateBased ): LocalDate = plus(- value, unit)
376+
377+ // workaround for https://youtrack.jetbrains.com/issue/KT-65484
378+ internal fun getIsoDateFormat () = LocalDate .Formats .ISO
0 commit comments