Skip to content

Commit

Permalink
Add rangeTo support for java.util.Date
Browse files Browse the repository at this point in the history
  • Loading branch information
moallemi authored Mar 23, 2020
1 parent e0f666f commit a3ecdb0
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/kotlin/me/moallemi/tools/daterange/date/DateExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package me.moallemi.tools.daterange.date

import java.util.Date

operator fun Date.rangeTo(other: Date) = DateRange(this, other)
27 changes: 27 additions & 0 deletions src/main/kotlin/me/moallemi/tools/daterange/date/DateIterator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package me.moallemi.tools.daterange.date

import java.util.Calendar
import java.util.Date
import java.util.Locale

class DateIterator(
startDate: Date,
private val endDate: Date,
private val stepDays: Int
) : Iterator<Date> {

private val calendar = Calendar.getInstance(Locale.getDefault())
private var currentDate = startDate

override fun hasNext() = currentDate <= endDate

override fun next(): Date {
val next = currentDate

calendar.time = currentDate
calendar.add(Calendar.DATE, stepDays)
currentDate = calendar.time

return next
}
}
15 changes: 15 additions & 0 deletions src/main/kotlin/me/moallemi/tools/daterange/date/DateRange.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.moallemi.tools.daterange.date

import java.util.Date

class DateRange(
override val start: Date,
override val endInclusive: Date,
private val stepDays: Int = 1
) : Iterable<Date>, ClosedRange<Date> {

override fun iterator(): Iterator<Date> =
DateIterator(start, endInclusive, stepDays)

infix fun step(days: Int) = DateRange(start, endInclusive, days)
}
53 changes: 53 additions & 0 deletions src/test/kotlin/me/moallemi/tools/daterange/date/DateRangeTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package me.moallemi.tools.daterange.date

import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
import junit.framework.TestCase.assertEquals
import org.junit.Test

class DateRangeTest {

private val calendar = Calendar.getInstance(Locale.getDefault())
private val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())

@Test
fun testRange() {
val expected = listOf(
"2020-01-01",
"2020-01-02",
"2020-01-03",
"2020-01-04",
"2020-01-05"
)
calendar.set(2020, 0, 1)
val startDate = calendar.time
calendar.set(2020, 0, 5)
val endDate = calendar.time

val actual = (startDate..endDate).map {
simpleDateFormat.format(it)
}

assertEquals(expected, actual)
}

@Test
fun testStep() {
val expected = listOf(
"2020-01-01",
"2020-01-03",
"2020-01-05"
)
calendar.set(2020, 0, 1)
val startDate = calendar.time
calendar.set(2020, 0, 5)
val endDate = calendar.time

val actual = (startDate..endDate step 2).map {
simpleDateFormat.format(it)
}

assertEquals(expected, actual)
}
}

0 comments on commit a3ecdb0

Please sign in to comment.