Last active
January 16, 2019 06:36
-
-
Save HarryTylenol/9d24a2c1556f4ed0d52fb612be495dc2 to your computer and use it in GitHub Desktop.
Android/Kotlin Date DSL
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
inline fun date(timeInMillis: Long = System.currentTimeMillis(), block: SimpleDate.() -> Unit = {}): SimpleDate { | |
val dateDsl = SimpleDate() | |
dateDsl.timeInMillis = timeInMillis | |
block(dateDsl) | |
return dateDsl | |
} | |
class SimpleDate { | |
private val calendar: Calendar = Calendar.getInstance() | |
var timeInMillis = System.currentTimeMillis() | |
get() = calendar.timeInMillis | |
set(value) { | |
calendar.timeInMillis = value | |
field = value | |
} | |
var date = Date() | |
get() = calendar.time | |
set(value) { | |
calendar.time = value | |
field = value | |
} | |
fun format(pattern: String, locale: Locale = Locale.US): String { | |
return SimpleDateFormat(pattern, locale).format(date) | |
} | |
val nextDay: SimpleDate get() = addDay(1) | |
val nextWeek: SimpleDate get() = addDay(7) | |
val nextYear: SimpleDate get() = addYear(1) | |
val nextNextMonth: SimpleDate get() = addMonth(1) | |
fun addDay(day: Int): SimpleDate { | |
this.day += day | |
return this | |
} | |
fun addYear(year: Int): SimpleDate { | |
this.year += year | |
return this | |
} | |
fun addMonth(month: Int): SimpleDate { | |
this.month += month | |
return this | |
} | |
fun addHour(hour: Int): SimpleDate { | |
this.hour += hour | |
return this | |
} | |
fun addMinute(minute: Int): SimpleDate { | |
this.minute += minute | |
return this | |
} | |
fun addSecond(second: Int): SimpleDate { | |
this.second += second | |
return this | |
} | |
fun addMillisecond(millisecond: Int): SimpleDate { | |
this.millisecond += millisecond | |
return this | |
} | |
var year = calendar.get(Calendar.YEAR) | |
set(value) { | |
calendar.set(Calendar.YEAR, value); field = value | |
} | |
var day = calendar.get(Calendar.DAY_OF_MONTH) | |
set(value) { | |
calendar.set(Calendar.DAY_OF_MONTH, value); field = value | |
} | |
var dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) | |
set(value) { | |
calendar.set(Calendar.DAY_OF_WEEK, value); field = value | |
} | |
var month = calendar.get(Calendar.MONTH) + 1 | |
set(value) { | |
calendar.set(Calendar.MONTH, value - 1); field = value | |
} | |
var hour = calendar.get(Calendar.HOUR) | |
set(value) { | |
calendar.set(Calendar.HOUR, value); field = value | |
} | |
var minute = calendar.get(Calendar.MINUTE) | |
set(value) { | |
calendar.set(Calendar.MINUTE, value); field = value | |
} | |
var second = calendar.get(Calendar.SECOND) | |
set(value) { | |
calendar.set(Calendar.SECOND, value); field = value | |
} | |
var millisecond = calendar.get(Calendar.MILLISECOND) | |
set(value) { | |
calendar.set(Calendar.MILLISECOND, value); field = value | |
} | |
} |
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
fun main() { | |
val now = date() | |
val nowAsDate : Date = now.date | |
val nowYear = now.year // 2018 | |
val nowMonth = now.month // 11 | |
val nowInMillis = now.timeInMillis | |
val hoursAheadFromNow = date().addHour(1) | |
val tomorrow = date().nextDay | |
val nextWeek = date().nextWeek | |
val nextYear = date().nextYear | |
val formattedTomorrow = tomorrow.format("yyyy MM dd hh:mm:ss") | |
val nextYearAprilFools = date { | |
month = 4; day = 1 | |
hour = 0; minute = 0; second = 0; millisecond = 0 | |
}.nextYear | |
val initTimeInMillis = date(timeInMillis = System.currentTimeMillis()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment