Skip to content

Commit 112e847

Browse files
committed
Implement YearMonth
Fixes #168 Fixes #184
1 parent 72681c2 commit 112e847

23 files changed

+1725
-449
lines changed

README.md

+18-4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ The library provides a basic set of types for working with date and time:
4040
- `Clock` to obtain the current instant;
4141
- `LocalDateTime` to represent date and time components without a reference to the particular time zone;
4242
- `LocalDate` to represent the components of date only;
43+
- `YearMonth` to represent only the year and month components;
4344
- `LocalTime` to represent the components of time only;
4445
- `TimeZone` and `FixedOffsetTimeZone` provide time zone information to convert between `Instant` and `LocalDateTime`;
4546
- `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
6768

6869
- Use `LocalDate` to represent the date of an event that does not have a specific time associated with it (like a birth date).
6970

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+
7074
- Use `LocalTime` to represent the time of an event that does not have a specific date associated with it.
7175

7276
## Operations
@@ -150,6 +154,16 @@ Note, that today's date really depends on the time zone in which you're observin
150154
val knownDate = LocalDate(2020, 2, 21)
151155
```
152156

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+
153167
### Getting local time components
154168

155169
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.
273287
```kotlin
274288
// import kotlinx.datetime.format.*
275289

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
280294

281295
val dateTimeOffset = DateTimeComponents.Formats.ISO_DATE_TIME_OFFSET
282296
.parse("2023-01-07T23:16:15.53+02:00")

0 commit comments

Comments
 (0)