@@ -69,41 +69,18 @@ import kotlinx.serialization.Serializable
69
69
* The recommended pattern is to convert a [LocalDateTime] to [Instant] as soon as possible (see
70
70
* [LocalDateTime.toInstant]) and work with [Instant] values instead.
71
71
*
72
- * [LocalDateTime] can be constructed directly from its components, [LocalDate] and [LocalTime], using the constructor:
73
- *
74
- * ```
75
- * val date = LocalDate(2021, 3, 27)
76
- * val time = LocalTime(hour = 2, minute = 16, second = 20)
77
- * LocalDateTime(date, time)
78
- * ```
72
+ * [LocalDateTime] can be constructed directly from its components, [LocalDate] and [LocalTime], using the constructor.
73
+ * See sample 1.
79
74
*
80
75
* Some additional constructors that accept the date's and time's fields directly are provided for convenience.
81
- *
82
- * ```
83
- * LocalDateTime(year = 2021, monthNumber = 3, dayOfMonth = 27, hour = 2, minute = 16, second = 20)
84
- * LocalDateTime(
85
- * year = 2021, month = Month.MARCH, dayOfMonth = 27,
86
- * hour = 2, minute = 16, second = 20, nanosecond = 999_999_999
87
- * )
88
- * ```
76
+ * See sample 2.
89
77
*
90
78
* [parse] and [toString] methods can be used to obtain a [LocalDateTime] from and convert it to a string in the
91
79
* ISO 8601 extended format (for example, `2023-01-02T22:35:01`).
92
- *
93
- * ```
94
- * LocalDateTime.parse("2023-01-02T22:35:01").toString() // 2023-01-02T22:35:01
95
- * ```
80
+ * See sample 3.
96
81
*
97
82
* [parse] and [LocalDateTime.format] both support custom formats created with [Format] or defined in [Formats].
98
- *
99
- * ```
100
- * val customFormat = LocalDateTime.Format {
101
- * date(LocalDate.Formats.ISO)
102
- * char(' ')
103
- * time(LocalTime.Formats.ISO)
104
- * }
105
- * LocalDateTime.parse("2023-01-02 22:35:01", customFormat).format(customFormat) // 2023-01-02 22:35:01
106
- * ```
83
+ * See sample 4.
107
84
*
108
85
* Additionally, there are several `kotlinx-serialization` serializers for [LocalDateTime]:
109
86
* - [LocalDateTimeIso8601Serializer] for the ISO 8601 extended format,
@@ -112,6 +89,10 @@ import kotlinx.serialization.Serializable
112
89
* @see LocalDate for only the date part of the date/time value.
113
90
* @see LocalTime for only the time part of the date/time value.
114
91
* @see Instant for the representation of a specific moment in time independent of a time zone.
92
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.fromDateAndTime
93
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.alternativeConstruction
94
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.simpleParsingAndFormatting
95
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.customFormat
115
96
*/
116
97
@Serializable(with = LocalDateTimeIso8601Serializer ::class )
117
98
public expect class LocalDateTime : Comparable <LocalDateTime > {
@@ -130,6 +111,8 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
130
111
*
131
112
* @throws IllegalArgumentException if the text cannot be parsed or the boundaries of [LocalDateTime] are
132
113
* exceeded.
114
+ *
115
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.parsing
133
116
*/
134
117
public fun parse (input : CharSequence , format : DateTimeFormat <LocalDateTime > = getIsoDateTimeFormat()): LocalDateTime
135
118
@@ -156,6 +139,8 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
156
139
* There is a collection of predefined formats in [LocalDateTime.Formats].
157
140
*
158
141
* @throws IllegalArgumentException if parsing using this format is ambiguous.
142
+ *
143
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.customFormat
159
144
*/
160
145
@Suppress(" FunctionName" )
161
146
public fun Format (builder : DateTimeFormatBuilder .WithDateTime .() -> Unit ): DateTimeFormat <LocalDateTime >
@@ -186,6 +171,8 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
186
171
* Fractional parts of the second are included if non-zero.
187
172
*
188
173
* Guaranteed to parse all strings that [LocalDateTime.toString] produces.
174
+ *
175
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.Formats.iso
189
176
*/
190
177
public val ISO : DateTimeFormat <LocalDateTime >
191
178
}
@@ -207,6 +194,8 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
207
194
*
208
195
* @throws IllegalArgumentException if any parameter is out of range,
209
196
* or if [dayOfMonth] is invalid for the given [monthNumber] and [year].
197
+ *
198
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.constructorFunctionWithMonthNumber
210
199
*/
211
200
public constructor (
212
201
year: Int ,
@@ -233,6 +222,8 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
233
222
*
234
223
* @throws IllegalArgumentException if any parameter is out of range,
235
224
* or if [dayOfMonth] is invalid for the given [month] and [year].
225
+ *
226
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.constructorFunction
236
227
*/
237
228
public constructor (
238
229
year: Int ,
@@ -246,43 +237,93 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
246
237
247
238
/* *
248
239
* Constructs a [LocalDateTime] instance by combining the given [date] and [time] parts.
240
+ *
241
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.fromDateAndTime
249
242
*/
250
243
public constructor (date: LocalDate , time: LocalTime )
251
244
252
- /* * Returns the year component of the date. */
245
+ /* *
246
+ * Returns the year component of the [date]. Can be negative.
247
+ *
248
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.dateComponents
249
+ */
253
250
public val year: Int
254
251
255
- /* * Returns the number-of-the-month (1..12) component of the date. */
252
+ /* *
253
+ * Returns the number-of-the-month (1..12) component of the [date].
254
+ *
255
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.dateComponents
256
+ */
256
257
public val monthNumber: Int
257
258
258
- /* * Returns the month ([Month]) component of the date. */
259
+ /* *
260
+ * Returns the month ([Month]) component of the [date].
261
+ *
262
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.dateComponents
263
+ */
259
264
public val month: Month
260
265
261
- /* * Returns the day-of-month component of the date. */
266
+ /* *
267
+ * Returns the day-of-month (`1..31`) component of the [date].
268
+ *
269
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.dateComponents
270
+ */
262
271
public val dayOfMonth: Int
263
272
264
- /* * Returns the day-of-week component of the date. */
273
+ /* *
274
+ * Returns the day-of-week component of the [date].
275
+ *
276
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.dateComponents
277
+ */
265
278
public val dayOfWeek: DayOfWeek
266
279
267
- /* * Returns the 1-based day-of-year component of the date. */
280
+ /* *
281
+ * Returns the 1-based day-of-year component of the [date].
282
+ *
283
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.dateComponents
284
+ */
268
285
public val dayOfYear: Int
269
286
270
- /* * Returns the hour-of-day time component of this date/time value. */
287
+ /* *
288
+ * Returns the hour-of-day (`0..59`) [time] component of this date/time value.
289
+ *
290
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.timeComponents
291
+ */
271
292
public val hour: Int
272
293
273
- /* * Returns the minute-of-hour time component of this date/time value. */
294
+ /* *
295
+ * Returns the minute-of-hour (`0..59`) [time] component of this date/time value.
296
+ *
297
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.timeComponents
298
+ */
274
299
public val minute: Int
275
300
276
- /* * Returns the second-of-minute time component of this date/time value. */
301
+ /* *
302
+ * Returns the second-of-minute (`0..59`) [time] component of this date/time value.
303
+ *
304
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.timeComponents
305
+ */
277
306
public val second: Int
278
307
279
- /* * Returns the nanosecond-of-second time component of this date/time value. */
308
+ /* *
309
+ * Returns the nanosecond-of-second (`0..999_999_999`) [time] component of this date/time value.
310
+ *
311
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.timeComponents
312
+ */
280
313
public val nanosecond: Int
281
314
282
- /* * Returns the date part of this date/time value. */
315
+ /* *
316
+ * Returns the date part of this date/time value.
317
+ *
318
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.dateAndTime
319
+ */
283
320
public val date: LocalDate
284
321
285
- /* * Returns the time part of this date/time value. */
322
+ /* *
323
+ * Returns the time part of this date/time value.
324
+ *
325
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.dateAndTime
326
+ */
286
327
public val time: LocalTime
287
328
288
329
/* *
@@ -300,6 +341,8 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
300
341
* val ldt2 = Clock.System.now().toLocalDateTime(zone) // 2021-10-31T02:01:20
301
342
* ldt2 > ldt1 // returns `false`
302
343
* ```
344
+ *
345
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.compareToSample
303
346
*/
304
347
public override operator fun compareTo (other : LocalDateTime ): Int
305
348
@@ -321,6 +364,7 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
321
364
* even if they are zero, and will not add trailing zeros to the fractional part of the second for readability.
322
365
* @see parse for the dual operation: obtaining [LocalDateTime] from a string.
323
366
* @see LocalDateTime.format for formatting using a custom format.
367
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.toStringSample
324
368
*/
325
369
public override fun toString (): String
326
370
}
@@ -330,6 +374,8 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
330
374
* Equivalent to calling [DateTimeFormat.format] on [format] with `this`.
331
375
*
332
376
* See [LocalDateTime.Formats] and [LocalDateTime.Format] for predefined and custom formats.
377
+ *
378
+ * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.formatting
333
379
*/
334
380
public fun LocalDateTime.format (format : DateTimeFormat <LocalDateTime >): String = format.format(this )
335
381
0 commit comments