Skip to content

Instantly share code, notes, and snippets.

@abdurahmanadilovic
Created January 20, 2018 10:01
Show Gist options
  • Save abdurahmanadilovic/99077936c3f308ef4d94c5f90cc79576 to your computer and use it in GitHub Desktop.
Save abdurahmanadilovic/99077936c3f308ef4d94c5f90cc79576 to your computer and use it in GitHub Desktop.
Extension function for "minutes ago" calendar formatting
// run the code below on https://try.kotlinlang.org with JUnit env
class CalendarUtilsTests{
@Test
fun testUtilsClass(){
val now = Calendar.getInstance()
val threeMinutesAgo = now.clone() as Calendar
threeMinutesAgo.add(Calendar.MINUTE, -3)
assertEquals("3 minutes ago", "${threeMinutesAgo.minutesAgoFrom(now)} minutes ago")
}
fun Calendar.minutesAgoFrom(before: Calendar): Long {
val diff = before.timeInMillis - this.timeInMillis
return TimeUnit.MILLISECONDS.toMinutes(diff)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment