Last active
April 18, 2023 20:03
-
-
Save bmc08gt/33a16fc3ef5bc13baceaffdd69c497f4 to your computer and use it in GitHub Desktop.
Helpful extensions for java.util.Date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.SimpleDateFormat | |
import java.util.* | |
import java.util.concurrent.TimeUnit | |
import kotlin.math.round | |
val now: Date = Date() | |
val Date.daysFromToday: Long | |
get() { | |
val msDiff = now.time - this.time | |
return TimeUnit.MILLISECONDS.toDays(msDiff) | |
} | |
fun Date.format(format: String): String = SimpleDateFormat(format, Locale.ENGLISH).format(this) | |
fun Date.startOfDay(): Date { | |
return Calendar.getInstance().apply { | |
this.add(Calendar.DATE, round(daysFromToday.toDouble()).toInt()) | |
this.setMin(Calendar.HOUR_OF_DAY) | |
this.setMin(Calendar.MINUTE) | |
this.setMin(Calendar.SECOND) | |
this.setMin(Calendar.MILLISECOND) | |
}.time | |
} | |
fun Date.endOfDay(): Date { | |
return Calendar.getInstance().apply { | |
this.add(Calendar.DATE, round(daysFromToday.toDouble()).toInt()) | |
this.setMax(Calendar.HOUR_OF_DAY) | |
this.setMax(Calendar.MINUTE) | |
this.setMax(Calendar.SECOND) | |
this.setMax(Calendar.MILLISECOND) | |
}.time | |
} | |
private fun Calendar.setMax(field: Int) { | |
set(field, getMaximum(field)) | |
} | |
private fun Calendar.setMin(field: Int) { | |
set(field, getMinimum(field)) | |
} | |
fun Date.minusHours(count: Int): Date { | |
return Calendar.getInstance().apply { | |
this.time = this@minusHours | |
this.add(Calendar.HOUR, -count) | |
}.time | |
} | |
fun Date.plusHours(count: Int): Date { | |
return Calendar.getInstance().apply { | |
this.time = this@plusHours | |
this.add(Calendar.HOUR, count) | |
}.time | |
} | |
fun Date.plusDays(count: Int): Date { | |
return Calendar.getInstance().apply { | |
this.time = this@plusDays | |
this.add(Calendar.DATE, count) | |
}.time | |
} | |
fun Date.minusDays(count: Int): Date { | |
return Calendar.getInstance().apply { | |
this.time = this@minusDays | |
this.add(Calendar.DATE, -count) | |
}.time | |
} | |
fun Date.minusMinutes(count: Int): Date { | |
return Calendar.getInstance().apply { | |
this.time = this@minusMinutes | |
this.add(Calendar.MINUTE, -count) | |
}.time | |
} | |
fun Date.plusMinutes(count: Int): Date { | |
return Calendar.getInstance().apply { | |
this.time = this@plusMinutes | |
this.add(Calendar.MINUTE, count) | |
}.time | |
} | |
fun Date.minusSeconds(count: Int): Date { | |
return Calendar.getInstance().apply { | |
this.time = this@minusSeconds | |
this.add(Calendar.SECOND, -count) | |
}.time | |
} | |
fun Date.plusSeconds(count: Int): Date { | |
return Calendar.getInstance().apply { | |
this.time = this@plusSeconds | |
this.add(Calendar.SECOND, count) | |
}.time | |
} | |
fun Date.hour(): Int { | |
return Calendar.getInstance().apply { | |
this.time = this@hour | |
}.get(Calendar.HOUR_OF_DAY) | |
} | |
fun Date.minutes(): Int { | |
return Calendar.getInstance().apply { | |
this.time = this@minutes | |
}.get(Calendar.MINUTE) | |
} | |
val Date.dayOfWeek: Int | |
get() = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) | |
fun Date.isSunday(): Boolean = Weekday.fromPosition(dayOfWeek) == Weekday.Sunday | |
fun Date.isMonday(): Boolean = Weekday.fromPosition(dayOfWeek) == Weekday.Monday | |
fun Date.isTuesday(): Boolean = Weekday.fromPosition(dayOfWeek) == Weekday.Tuesday | |
fun Date.isWednesday(): Boolean = Weekday.fromPosition(dayOfWeek) == Weekday.Wednesday | |
fun Date.isThursday(): Boolean = Weekday.fromPosition(dayOfWeek) == Weekday.Thursday | |
fun Date.isFriday(): Boolean = Weekday.fromPosition(dayOfWeek) == Weekday.Friday | |
fun Date.isSaturday(): Boolean = Weekday.fromPosition(dayOfWeek) == Weekday.Saturday | |
sealed class Weekday(val weekPosition: Int) { | |
object Sunday : Weekday(0) | |
object Monday : Weekday(1) | |
object Tuesday : Weekday(2) | |
object Wednesday : Weekday(3) | |
object Thursday : Weekday(4) | |
object Friday : Weekday(5) | |
object Saturday : Weekday(6) | |
object Unknown : Weekday(-1) | |
companion object { | |
fun fromPosition(pos: Int): Weekday { | |
return Weekday::class.nestedClasses | |
.map { klass -> klass.objectInstance } | |
.filterIsInstance<Weekday>() | |
.find { it.weekPosition == pos } ?: Unknown | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment