Skip to content

Instantly share code, notes, and snippets.

@shashankdaima
Last active November 24, 2021 11:41
Show Gist options
  • Save shashankdaima/26c8c6229b9a1eb868b19573abfd3c49 to your computer and use it in GitHub Desktop.
Save shashankdaima/26c8c6229b9a1eb868b19573abfd3c49 to your computer and use it in GitHub Desktop.
Time mapper.
import java.util.*
/*
* This is something that i wrote purely because of requirement in future i will improve on this.
* */
class DateTime {
lateinit var date: Triple<Int, Int, Int> //FORMAT: DATE/MONTH/YEAR
lateinit var time: Triple<Int, Int, Int> //FORMAT: HOURS:MINUTES:SECONDS
var epoch: Long = 0
constructor(epoch: Long) {
this.epoch = epoch
val cal = Calendar.getInstance().apply {
timeInMillis = epoch
}
this.date = Triple(cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.YEAR))
this.time = Triple(cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND))
}
constructor(date: Int, month: Int, year: Int) {
this.date = Triple(date, month, year)
this.time = Triple(0, 0, 0)
val calendar = Calendar.getInstance()
calendar.set(Calendar.YEAR, year)
calendar.set(Calendar.MONTH, month - 1)
calendar.set(Calendar.DAY_OF_MONTH, date)
calendar.set(Calendar.HOUR_OF_DAY, 0)
calendar.set(Calendar.MINUTE, 0)
calendar.set(Calendar.SECOND, 0)
calendar.set(Calendar.MILLISECOND, 0)
epoch = calendar.time.time
}
constructor(date: Int, month: Int, year: Int, hours: Int, minute: Int, seconds: Int) {
this.date = Triple(date, month, year)
this.time = Triple(hours, minute, seconds)
val calendar = Calendar.getInstance()
calendar.set(Calendar.YEAR, year)
calendar.set(Calendar.MONTH, month - 1)
calendar.set(Calendar.DAY_OF_MONTH, date)
calendar.set(Calendar.HOUR_OF_DAY, hours)
calendar.set(Calendar.MINUTE, minute)
calendar.set(Calendar.SECOND, seconds)
calendar.set(Calendar.MILLISECOND, 0)
epoch = calendar.time.time
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment