Created
January 20, 2018 10:01
-
-
Save abdurahmanadilovic/99077936c3f308ef4d94c5f90cc79576 to your computer and use it in GitHub Desktop.
Extension function for "minutes ago" calendar formatting
This file contains hidden or 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
// 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