@@ -40,6 +40,7 @@ The library provides a basic set of types for working with date and time:
40
40
- ` Clock ` to obtain the current instant;
41
41
- ` LocalDateTime ` to represent date and time components without a reference to the particular time zone;
42
42
- ` LocalDate ` to represent the components of date only;
43
+ - ` YearMonth ` to represent only the year and month components;
43
44
- ` LocalTime ` to represent the components of time only;
44
45
- ` TimeZone ` and ` FixedOffsetTimeZone ` provide time zone information to convert between ` Instant ` and ` LocalDateTime ` ;
45
46
- ` Month ` and ` DayOfWeek ` enums;
@@ -67,6 +68,9 @@ Here is some basic advice on how to choose which of the date-carrying types to u
67
68
68
69
- Use ` LocalDate ` to represent the date of an event that does not have a specific time associated with it (like a birth date).
69
70
71
+ - Use ` YearMonth ` to represent the year and month of an event that does not have a specific day associated with it
72
+ or has a day-of-month that is inferred from the context (like a credit card expiration date).
73
+
70
74
- Use ` LocalTime ` to represent the time of an event that does not have a specific date associated with it.
71
75
72
76
## Operations
@@ -150,6 +154,16 @@ Note, that today's date really depends on the time zone in which you're observin
150
154
val knownDate = LocalDate (2020 , 2 , 21 )
151
155
```
152
156
157
+ ### Getting year and month components
158
+
159
+ A ` YearMonth ` represents a year and month without a day. You can obtain one from a ` LocalDate `
160
+ by taking its ` yearMonth ` property.
161
+
162
+ ``` kotlin
163
+ val day = LocalDate (2020 , 2 , 21 )
164
+ val yearMonth: YearMonth = day.yearMonth
165
+ ```
166
+
153
167
### Getting local time components
154
168
155
169
A ` LocalTime ` represents local time without date. You can obtain one from an ` Instant `
@@ -273,10 +287,10 @@ collection of all datetime fields, can be used instead.
273
287
``` kotlin
274
288
// import kotlinx.datetime.format.*
275
289
276
- val yearMonth = DateTimeComponents .Format { year (); char(' - ' ); monthNumber () }
277
- .parse(" 2024-01 " )
278
- println (yearMonth.year)
279
- println (yearMonth .monthNumber)
290
+ val monthDay = DateTimeComponents .Format { monthNumber (); char(' / ' ); dayOfMonth () }
291
+ .parse(" 12/25 " )
292
+ println (monthDay.dayOfMonth) // 25
293
+ println (monthDay .monthNumber) // 12
280
294
281
295
val dateTimeOffset = DateTimeComponents .Formats .ISO_DATE_TIME_OFFSET
282
296
.parse(" 2023-01-07T23:16:15.53+02:00" )
0 commit comments