Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/moallemi/kotlin-date-range
Implementation of rangeTo operator for LocalDate and Date in kotlin
https://github.com/moallemi/kotlin-date-range
hacktoberfest kotlin kotlin-jvm localdate
Last synced: 6 days ago
JSON representation
Implementation of rangeTo operator for LocalDate and Date in kotlin
- Host: GitHub
- URL: https://github.com/moallemi/kotlin-date-range
- Owner: moallemi
- License: apache-2.0
- Created: 2020-03-22T12:30:43.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-10-25T04:54:08.000Z (about 2 years ago)
- Last Synced: 2024-05-02T01:43:13.152Z (7 months ago)
- Topics: hacktoberfest, kotlin, kotlin-jvm, localdate
- Language: Kotlin
- Homepage:
- Size: 88.9 KB
- Stars: 41
- Watchers: 4
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Kotlin Date Range
[![GitHub Workflow Status](https://github.com/moallemi/kotlin-date-range/workflows/CI/badge.svg)](https://github.com/moallemi/kotlin-date-range/actions?query=workflow%3ACI)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/me.moallemi.tools/kotlin-date-range/badge.svg)](https://search.maven.org/artifact/me.moallemi.tools/kotlin-date-range)This is a tiny implementation of `rangeTo` operator for `Date` and `LocalDate` that makes the syntax for loop iteration and control flow statements safe and natural to read.
## Installation
Add the kotlin-date-range to your dependencies section:
```groovy
dependencies {
implementation 'me.moallemi.tools:kotlin-date-range:1.0.0'
}
```## How to use
From Java SE 8 onwards, users are asked to migrate to java.time (JSR-310). For Android users, java.time is [added in API level 26+](https://developer.android.com/reference/java/time/package-summary). Projects needing to support lower API levels can use `java.util.Date`.
#### with `java.time.LocalDate`
Using `rangeTo` operator:
```kotlin
import me.moallemi.tools.daterange.localdate.rangeToval startDate = LocalDate.of(2020, 3, 22)
val endDate = LocalDate.of(2020, 4, 1)
for (date in startDate..endDate) {
println(date)
}
```Usage with `step` function:
```kotlin
import me.moallemi.tools.daterange.localdate.rangeToval startDate = LocalDate.of(2020, 3, 22)
val endDate = LocalDate.of(2020, 4, 1)
for (date in startDate..endDate step 2) {
println(date)
}
```Using with `in` operator:
```kotlin
import me.moallemi.tools.daterange.localdate.rangeToval startDate = LocalDate.of(2020, 1, 1)
val endDate = LocalDate.of(2020, 1, 5)val result: Boolean = LocalDate.of(2020, 1, 2) in (startDate..endDate)
```#### with `java.util.Date`
Using `rangeTo` operator:
```kotlin
import me.moallemi.tools.daterange.date.rangeToval calendar = Calendar.getInstance(Locale.getDefault())
calendar.set(2020, 0, 1)
val startDate = calendar.time
calendar.set(2020, 0, 5)
val endDate = calendar.time
for (date in startDate..endDate) {
println(date)
}
```Usage with `step` function:
```kotlin
import me.moallemi.tools.daterange.date.rangeTofor (date in startDate..endDate step 2) {
println(date)
}
```## License
```
Copyright 2020 Reza Moallemi.Licensed to the Apache Software Foundation (ASF) under one or more contributor
license agreements. See the NOTICE file distributed with this work for
additional information regarding copyright ownership. The ASF licenses this
file to you under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License athttp://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
```