Created
July 17, 2018 07:41
-
-
Save iRYO400/3a452a69b8afae8647e9582b688b84c6 to your computer and use it in GitHub Desktop.
A-snippet #2 - Relative time converting
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
const val TIMESTAMP_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" // eg. 2018-04-01T04:20:25.434Z | |
/** | |
* Time converting from any timestamp pattern to "5 minutes ago", "1 day ago", etc. | |
*/ | |
fun convertTime(timestamp: String): String { | |
val mDataFormat: DateFormat = SimpleDateFormat(TIMESTAMP_PATTERN, Locale.ENGLISH) | |
return try { | |
val date = mDataFormat.parse(timestamp) | |
val relativeDateStr = DateUtils.getRelativeTimeSpanString(date.time, Calendar.getInstance().timeInMillis, DateUtils.MINUTE_IN_MILLIS) | |
relativeDateStr.toString() | |
} catch (e: ParseException) { | |
Log.e("ParseException", "Unparseable date " + e.message) | |
timestamp | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment