I am trying to calculate the time between two dates and show the difference in time in the UI as x mins ago
, x days ago
, x weeks ago
etc (depending on the length of time).
The first date is a published date which is given to me by the api. I then format this date to yyyy-MM-dd HH:mm:ss
. Second date is the current time.
val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
sdf.setTimeZone(TimeZone.getTimeZone("GMT"))
val time = sdf.parse("$year-$month-$day $hours").time // 2019-03-14 13:00:55
val now = System.currentTimeMillis()
val relative = DateUtils.getRelativeTimeSpanString(time, now, 0L, DateUtils.FORMAT_ABBREV_ALL)
However, when I print relative
, I get Mar 14
when I would expect x months ago
.
Can't tell what I'm missing apart from I have read somewhere that getRelativeTimeSpanString() only works for 7 days, but the documentation doesn't state this so not sure if that's the case?
Any help to solve this would be appreciated!