All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.customer.base.extenstions.DateExtensions.kt Maven / Gradle / Ivy

There is a newer version: 4.2.0
Show newest version
package io.customer.base.extenstions

import java.util.*
import java.util.concurrent.TimeUnit

fun Date.getUnixTimestamp(): Long {
    return TimeUnit.MILLISECONDS.toSeconds(this.time)
}

fun Long.unixTimeToDate(): Date {
    val seconds = this
    val milliseconds = TimeUnit.SECONDS.toMillis(seconds)
    return Date(milliseconds)
}

fun Date.add(unit: Int, type: TimeUnit): Date = this.add(unit.toLong(), type)
fun Date.add(unit: Long, type: TimeUnit): Date {
    return Date(this.time + type.toMillis(unit))
}

fun Date.subtract(unit: Double, type: TimeUnit): Date = this.subtract(unit.toLong(), type)
fun Date.subtract(unit: Int, type: TimeUnit): Date = this.subtract(unit.toLong(), type)
fun Date.subtract(unit: Long, type: TimeUnit): Date {
    return Date(this.time - type.toMillis(unit))
}

fun Date.hasPassed(): Boolean {
    return this.time < Date().time
}

fun Date.isOlderThan(otherDate: Date): Boolean {
    return this.time < otherDate.time
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy