Coder Social home page Coder Social logo

kotlin-date-range's Introduction

Kotlin Date Range

GitHub Workflow Status Maven Central

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:

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+. Projects needing to support lower API levels can use java.util.Date.

with java.time.LocalDate

Using rangeTo operator:

import me.moallemi.tools.daterange.localdate.rangeTo

val startDate = LocalDate.of(2020, 3, 22)
val endDate = LocalDate.of(2020, 4, 1)
for (date in startDate..endDate) {
    println(date)
}

Usage with step function:

import me.moallemi.tools.daterange.localdate.rangeTo

val 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:

import me.moallemi.tools.daterange.localdate.rangeTo

val 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:

import me.moallemi.tools.daterange.date.rangeTo

val 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:

import me.moallemi.tools.daterange.date.rangeTo

for (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 at

   http://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.

kotlin-date-range's People

Contributors

moallemi avatar wolflee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

kotlin-date-range's Issues

[Discussion] Add syntax sugar for LocalDateTime.rangeTo(LocalDate)

val myTime: LocalDateTime

val range = myTime..LocalDate.now()

This currently isn't possible, and we need to adapt myTime to get it's LocalDate part:

val range = myTime.toLocalDate()..LocalDate.now()

I'd love to not have to do this, thus adding a syntax sugar can help in that way.

However, I can see how this is very error-prone, so I"d like to open this one for discussion.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.