@@ -32,44 +32,27 @@ import kotlinx.serialization.Serializable
32
32
* ### Construction, serialization, and deserialization
33
33
*
34
34
* [LocalDate] can be constructed directly from its components, using the constructor.
35
- *
36
- * ```
37
- * LocalDate(year = 2023, monthNumber = 1, dayOfMonth = 2) == LocalDate(2023, Month.JANUARY, 2)
38
- * ```
35
+ * See sample 1.
39
36
*
40
37
* [fromEpochDays] can be used to obtain a [LocalDate] from the number of days since the epoch day `1970-01-01`;
41
38
* [toEpochDays] is the inverse operation.
42
- *
43
- * ```
44
- * LocalDate.fromEpochDays(0) == LocalDate(1970, Month.JANUARY, 1)
45
- * LocalDate(1970, Month.JANUARY, 31).toEpochDays() == 30
46
- * ```
39
+ * See sample 2.
47
40
*
48
41
* [parse] and [toString] methods can be used to obtain a [LocalDate] from and convert it to a string in the
49
42
* ISO 8601 extended format.
50
- *
51
- * ```
52
- * LocalDate.parse("2023-01-02") == LocalDate(2023, Month.JANUARY, 2)
53
- * LocalDate(2023, Month.JANUARY, 2).toString() == "2023-01-02"
54
- * ```
43
+ * See sample 3.
55
44
*
56
45
* [parse] and [LocalDate.format] both support custom formats created with [Format] or defined in [Formats].
57
- *
58
- * ```
59
- * val customFormat = LocalDate.Format {
60
- * monthName(MonthNames.ENGLISH_ABBREVIATED)
61
- * char(' ')
62
- * dayOfMonth()
63
- * char(' ')
64
- * year()
65
- * }
66
- * LocalDate.parse("Jan 05 2020", customFormat) == LocalDate(2020, Month.JANUARY, 5)
67
- * LocalDate(2020, Month.JANUARY, 5).format(customFormat) == "Jan 05 2020"
68
- * ```
46
+ * See sample 4.
69
47
*
70
48
* Additionally, there are several `kotlinx-serialization` serializers for [LocalDate]:
71
49
* - [LocalDateIso8601Serializer] for the ISO 8601 extended format,
72
50
* - [LocalDateComponentSerializer] for an object with components.
51
+ *
52
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.constructorFunctionMonthNumber
53
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.fromAndToEpochDays
54
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.simpleParsingAndFormatting
55
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.customFormat
73
56
*/
74
57
@Serializable(with = LocalDateIso8601Serializer ::class )
75
58
public expect class LocalDate : Comparable <LocalDate > {
@@ -86,39 +69,29 @@ public expect class LocalDate : Comparable<LocalDate> {
86
69
*
87
70
* @see LocalDate.toString for formatting using the default format.
88
71
* @see LocalDate.format for formatting using a custom format.
72
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.parsing
89
73
*/
90
74
public fun parse (input : CharSequence , format : DateTimeFormat <LocalDate > = getIsoDateFormat()): LocalDate
91
75
92
76
/* *
93
77
* Returns a [LocalDate] that is [epochDays] number of days from the epoch day `1970-01-01`.
94
78
*
95
79
* @throws IllegalArgumentException if the result exceeds the platform-specific boundaries of [LocalDate].
96
- *
97
80
* @see LocalDate.toEpochDays
81
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.fromAndToEpochDays
98
82
*/
99
83
public fun fromEpochDays (epochDays : Int ): LocalDate
100
84
101
85
/* *
102
86
* Creates a new format for parsing and formatting [LocalDate] values.
103
87
*
104
- * Example:
105
- * ```
106
- * // 2020 Jan 05
107
- * LocalDate.Format {
108
- * year()
109
- * char(' ')
110
- * monthName(MonthNames.ENGLISH_ABBREVIATED)
111
- * char(' ')
112
- * dayOfMonth()
113
- * }
114
- * ```
115
- *
116
88
* Only parsing and formatting of well-formed values is supported. If the input does not fit the boundaries
117
89
* (for example, [dayOfMonth] is 31 for February), consider using [DateTimeComponents.Format] instead.
118
90
*
119
91
* There is a collection of predefined formats in [LocalDate.Formats].
120
92
*
121
93
* @throws IllegalArgumentException if parsing using this format is ambiguous.
94
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.customFormat
122
95
*/
123
96
@Suppress(" FunctionName" )
124
97
public fun Format (block : DateTimeFormatBuilder .WithDate .() -> Unit ): DateTimeFormat <LocalDate >
@@ -146,6 +119,8 @@ public expect class LocalDate : Comparable<LocalDate> {
146
119
* - `+12020-08-30`
147
120
* - `0000-08-30`
148
121
* - `-0001-08-30`
122
+ *
123
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.Formats.iso
149
124
*/
150
125
public val ISO : DateTimeFormat <LocalDate >
151
126
@@ -157,6 +132,8 @@ public expect class LocalDate : Comparable<LocalDate> {
157
132
* - `+120200830`
158
133
* - `00000830`
159
134
* - `-00010830`
135
+ *
136
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.Formats.isoBasic
160
137
*/
161
138
public val ISO_BASIC : DateTimeFormat <LocalDate >
162
139
}
@@ -174,6 +151,7 @@ public expect class LocalDate : Comparable<LocalDate> {
174
151
*
175
152
* @throws IllegalArgumentException if any parameter is out of range, or if [dayOfMonth] is invalid for the
176
153
* given [monthNumber] and [year].
154
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.constructorFunctionMonthNumber
177
155
*/
178
156
public constructor (year: Int , monthNumber: Int , dayOfMonth: Int )
179
157
@@ -188,25 +166,50 @@ public expect class LocalDate : Comparable<LocalDate> {
188
166
*
189
167
* @throws IllegalArgumentException if any parameter is out of range, or if [dayOfMonth] is invalid for the
190
168
* given [month] and [year].
169
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.constructorFunction
191
170
*/
192
171
public constructor (year: Int , month: Month , dayOfMonth: Int )
193
172
194
- /* * Returns the year component of the date. */
173
+ /* *
174
+ * Returns the year component of the date.
175
+ *
176
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.year
177
+ */
195
178
public val year: Int
196
179
197
- /* * Returns the number-of-the-month (1..12) component of the date. */
180
+ /* *
181
+ * Returns the number-of-the-month (1..12) component of the date.
182
+ *
183
+ * Shortcut for `month.number`.
184
+ */
198
185
public val monthNumber: Int
199
186
200
- /* * Returns the month ([Month]) component of the date. */
187
+ /* *
188
+ * Returns the month ([Month]) component of the date.
189
+ *
190
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.month
191
+ */
201
192
public val month: Month
202
193
203
- /* * Returns the day-of-month component of the date. */
194
+ /* *
195
+ * Returns the day-of-month (`1..31`) component of the date.
196
+ *
197
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.dayOfMonth
198
+ */
204
199
public val dayOfMonth: Int
205
200
206
- /* * Returns the day-of-week component of the date. */
201
+ /* *
202
+ * Returns the day-of-week component of the date.
203
+ *
204
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.dayOfWeek
205
+ */
207
206
public val dayOfWeek: DayOfWeek
208
207
209
- /* * Returns the day-of-year component of the date. */
208
+ /* *
209
+ * Returns the day-of-year (`1..366`) component of the date.
210
+ *
211
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.dayOfYear
212
+ */
210
213
public val dayOfYear: Int
211
214
212
215
/* *
@@ -215,6 +218,7 @@ public expect class LocalDate : Comparable<LocalDate> {
215
218
* If the result does not fit in [Int], returns [Int.MAX_VALUE] for a positive result or [Int.MIN_VALUE] for a negative result.
216
219
*
217
220
* @see LocalDate.fromEpochDays
221
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.toEpochDays
218
222
*/
219
223
public fun toEpochDays (): Int
220
224
@@ -223,6 +227,8 @@ public expect class LocalDate : Comparable<LocalDate> {
223
227
* Returns zero if this date represents the same day as the other (i.e., equal to other),
224
228
* a negative number if this date is earlier than the other,
225
229
* and a positive number if this date is later than the other.
230
+ *
231
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.compareToSample
226
232
*/
227
233
public override fun compareTo (other : LocalDate ): Int
228
234
@@ -232,13 +238,16 @@ public expect class LocalDate : Comparable<LocalDate> {
232
238
* @see Formats.ISO for the format details.
233
239
* @see parse for the dual operation: obtaining [LocalDate] from a string.
234
240
* @see LocalDate.format for formatting using a custom format.
241
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.toStringSample
235
242
*/
236
243
public override fun toString (): String
237
244
}
238
245
239
246
/* *
240
247
* Formats this value using the given [format].
241
248
* Equivalent to calling [DateTimeFormat.format] on [format] with `this`.
249
+ *
250
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.formatting
242
251
*/
243
252
public fun LocalDate.format (format : DateTimeFormat <LocalDate >): String = format.format(this )
244
253
@@ -253,6 +262,8 @@ public fun String.toLocalDate(): LocalDate = LocalDate.parse(this)
253
262
*
254
263
* For finding an instant that corresponds to the start of a date in a particular time zone consider using
255
264
* [LocalDate.atStartOfDayIn] function because a day does not always start at the fixed time 0:00:00.
265
+ *
266
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.atTimeInline
256
267
*/
257
268
public fun LocalDate.atTime (hour : Int , minute : Int , second : Int = 0, nanosecond : Int = 0): LocalDateTime =
258
269
LocalDateTime (year, monthNumber, dayOfMonth, hour, minute, second, nanosecond)
@@ -267,6 +278,8 @@ public fun LocalDate.atTime(hour: Int, minute: Int, second: Int = 0, nanosecond:
267
278
* exist in the implicit time zone.
268
279
* For example, `LocalDate(2021, 3, 28).atTime(LocalTime(2, 16, 20))` will successfully create a [LocalDateTime],
269
280
* even though in Berlin, times between 2:00 and 3:00 do not exist on March 28, 2021 due to the transition to DST.
281
+ *
282
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.atTime
270
283
*/
271
284
public fun LocalDate.atTime (time : LocalTime ): LocalDateTime = LocalDateTime (this , time)
272
285
@@ -275,33 +288,21 @@ public fun LocalDate.atTime(time: LocalTime): LocalDateTime = LocalDateTime(this
275
288
* Returns a date that is the result of adding components of [DatePeriod] to this date. The components are
276
289
* added in the order from the largest units to the smallest: first years and months, then days.
277
290
*
278
- * ```
279
- * LocalDate(2023, Month.JANUARY, 30) + DatePeriod(years = 1, months = 2, days = 2) == LocalDate(2024, Month.APRIL, 1)
280
- * // 2023-01-30 + 1 year = 2024-01-30
281
- * // 2024-01-30 + 2 months = 2024-03-30
282
- * // 2024-03-30 + 2 days = 2024-04-01
283
- * ```
284
- *
285
291
* @see LocalDate.periodUntil
286
292
* @throws DateTimeArithmeticException if this value or the results of intermediate computations are too large to fit in
287
293
* [LocalDate].
294
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.plusPeriod
288
295
*/
289
296
public expect operator fun LocalDate.plus (period : DatePeriod ): LocalDate
290
297
291
298
/* *
292
299
* Returns a date that is the result of subtracting components of [DatePeriod] from this date. The components are
293
300
* subtracted in the order from the largest units to the smallest: first years and months, then days.
294
301
*
295
- * ```
296
- * LocalDate(2023, Month.JANUARY, 2) - DatePeriod(years = 1, months = 2, days = 3) == LocalDate(2021, Month.OCTOBER, 30)
297
- * // 2023-01-02 - 1 year = 2022-01-02
298
- * // 2022-01-02 - 2 months = 2021-11-02
299
- * // 2021-11-02 - 3 days = 2021-10-30
300
- * ```
301
- *
302
302
* @see LocalDate.periodUntil
303
303
* @throws DateTimeArithmeticException if this value or the results of intermediate computations are too large to fit in
304
304
* [LocalDate].
305
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.minusPeriod
305
306
*/
306
307
public operator fun LocalDate.minus (period : DatePeriod ): LocalDate =
307
308
if (period.days != Int .MIN_VALUE && period.months != Int .MIN_VALUE ) {
@@ -322,13 +323,10 @@ public operator fun LocalDate.minus(period: DatePeriod): LocalDate =
322
323
* - negative or zero if this date is later than the other,
323
324
* - exactly zero if this date is equal to the other.
324
325
*
325
- * ```
326
- * LocalDate(2023, Month.JANUARY, 2).periodUntil(LocalDate(2024, Month.APRIL, 1)) == DatePeriod(years = 1, months = 2, days = 30)
327
- * ```
328
- *
329
326
* @throws DateTimeArithmeticException if the number of months between the two dates exceeds an Int (JVM only).
330
327
*
331
328
* @see LocalDate.minus for the same operation with the order of arguments reversed.
329
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.periodUntil
332
330
*/
333
331
public expect fun LocalDate.periodUntil (other : LocalDate ): DatePeriod
334
332
@@ -342,13 +340,10 @@ public expect fun LocalDate.periodUntil(other: LocalDate): DatePeriod
342
340
* - positive or zero if this date is later than the other,
343
341
* - exactly zero if this date is equal to the other.
344
342
*
345
- * ```
346
- * LocalDate(2024, Month.APRIL, 1) - LocalDate(2023, Month.JANUARY, 2) == DatePeriod(years = 1, months = 2, days = 30)
347
- * ```
348
- *
349
343
* @throws DateTimeArithmeticException if the number of months between the two dates exceeds an Int (JVM only).
350
344
*
351
345
* @see LocalDate.periodUntil for the same operation with the order of arguments reversed.
346
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.minusDate
352
347
*/
353
348
public operator fun LocalDate.minus (other : LocalDate ): DatePeriod = other.periodUntil(this )
354
349
@@ -361,18 +356,13 @@ public operator fun LocalDate.minus(other: LocalDate): DatePeriod = other.period
361
356
* - zero if this date is equal to the other.
362
357
*
363
358
* The value is rounded toward zero.
364
- *
365
- * ```
366
- * LocalDate(2023, Month.JANUARY, 2).until(LocalDate(2024, Month.APRIL, 1), DateTimeUnit.MONTH) == 14
367
- * // one year, two months, and 30 days, rounded toward zero.
368
- * ```
369
359
370
360
* If the result does not fit in [Int], returns [Int.MAX_VALUE] for a positive result or [Int.MIN_VALUE] for a negative result.
371
361
*
372
362
* @see LocalDate.daysUntil
373
363
* @see LocalDate.monthsUntil
374
364
* @see LocalDate.yearsUntil
375
- *
365
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.until
376
366
*/
377
367
public expect fun LocalDate.until (other : LocalDate , unit : DateTimeUnit .DateBased ): Int
378
368
@@ -384,6 +374,7 @@ public expect fun LocalDate.until(other: LocalDate, unit: DateTimeUnit.DateBased
384
374
* If the result does not fit in [Int], returns [Int.MAX_VALUE] for a positive result or [Int.MIN_VALUE] for a negative result.
385
375
*
386
376
* @see LocalDate.until
377
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.daysUntil
387
378
*/
388
379
public expect fun LocalDate.daysUntil (other : LocalDate ): Int
389
380
@@ -395,6 +386,7 @@ public expect fun LocalDate.daysUntil(other: LocalDate): Int
395
386
* If the result does not fit in [Int], returns [Int.MAX_VALUE] for a positive result or [Int.MIN_VALUE] for a negative result.
396
387
*
397
388
* @see LocalDate.until
389
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.monthsUntil
398
390
*/
399
391
public expect fun LocalDate.monthsUntil (other : LocalDate ): Int
400
392
@@ -404,6 +396,7 @@ public expect fun LocalDate.monthsUntil(other: LocalDate): Int
404
396
* If the result does not fit in [Int], returns [Int.MAX_VALUE] for a positive result or [Int.MIN_VALUE] for a negative result.
405
397
*
406
398
* @see LocalDate.until
399
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.yearsUntil
407
400
*/
408
401
public expect fun LocalDate.yearsUntil (other : LocalDate ): Int
409
402
@@ -440,6 +433,7 @@ public fun LocalDate.minus(unit: DateTimeUnit.DateBased): LocalDate = plus(-1, u
440
433
* The value is rounded toward zero.
441
434
*
442
435
* @throws DateTimeArithmeticException if the result exceeds the boundaries of [LocalDate].
436
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.plusInt
443
437
*/
444
438
public expect fun LocalDate.plus (value : Int , unit : DateTimeUnit .DateBased ): LocalDate
445
439
@@ -452,6 +446,7 @@ public expect fun LocalDate.plus(value: Int, unit: DateTimeUnit.DateBased): Loca
452
446
* The value is rounded toward zero.
453
447
*
454
448
* @throws DateTimeArithmeticException if the result exceeds the boundaries of [LocalDate].
449
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.minusInt
455
450
*/
456
451
public expect fun LocalDate.minus (value : Int , unit : DateTimeUnit .DateBased ): LocalDate
457
452
@@ -464,6 +459,7 @@ public expect fun LocalDate.minus(value: Int, unit: DateTimeUnit.DateBased): Loc
464
459
* The value is rounded toward zero.
465
460
*
466
461
* @throws DateTimeArithmeticException if the result exceeds the boundaries of [LocalDate].
462
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.plusLong
467
463
*/
468
464
public expect fun LocalDate.plus (value : Long , unit : DateTimeUnit .DateBased ): LocalDate
469
465
@@ -476,6 +472,7 @@ public expect fun LocalDate.plus(value: Long, unit: DateTimeUnit.DateBased): Loc
476
472
* The value is rounded toward zero.
477
473
*
478
474
* @throws DateTimeArithmeticException if the result exceeds the boundaries of [LocalDate].
475
+ * @sample kotlinx.datetime.test.samples.LocalDateSamples.minusLong
479
476
*/
480
477
public fun LocalDate.minus (value : Long , unit : DateTimeUnit .DateBased ): LocalDate = plus(- value, unit)
481
478
0 commit comments