Skip to content

Instantly share code, notes, and snippets.

@TroyStopera
Created September 20, 2019 17:31
Show Gist options
  • Save TroyStopera/7f943c1aa2a0f55e0afabfbc436dc74e to your computer and use it in GitHub Desktop.
Save TroyStopera/7f943c1aa2a0f55e0afabfbc436dc74e to your computer and use it in GitHub Desktop.
Date utils
import java.util.*
fun Date.isSameDay(other: Date, timeZone: TimeZone = TimeZone.getDefault()): Boolean {
val cal1 = Calendar.getInstance(timeZone).also { it.time = this }
val cal2 = Calendar.getInstance(timeZone).also { it.time = other }
return cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR) &&
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
}
fun Date.isYesterday(timeZone: TimeZone = TimeZone.getDefault()): Boolean {
val yesterday = Calendar.getInstance(timeZone).apply { add(Calendar.DAY_OF_YEAR, -1) }
return this.isSameDay(yesterday.time, timeZone)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment