Home > Mobile >  Will Android DateUtils.getRelativeTimeSpanString format relative past datetimes to required statemen
Will Android DateUtils.getRelativeTimeSpanString format relative past datetimes to required statemen

Time:01-12

In my current Android project I have a requirement to display historical string datetimes that have the following format/value examples

2023-01-06T14:52:41.633
2023-01-06T00:00:00
2022-12-23T00:00:00
2022-12-22T11:17:43.94

into string "phrases" such as

Today
1 Day Ago
3 Days Ago
1 Week Ago
4 Weeks Ago
1 Month Ago
5 Months Ago
1 Year Ago

I thought android.text.format.DateUtils would provide the solution however the results I am seeing are wrong

the code I am using is as follows

            val then = LocalDateTime.parse(productionDate)
            doLogging{"xxooxx past date = ${productionDate}"}

            doLogging{"xxooxx ${DateUtils.getRelativeTimeSpanString(then.toEpochSecond(ZoneOffset.UTC), LocalDateTime.now().toEpochSecond(ZoneOffset.UTC),0L, DateUtils.FORMAT_ABBREV_RELATIVE)}"}
            doLogging{"xxooxx \t\t ${DateUtils.getRelativeTimeSpanString(then.toEpochSecond(ZoneOffset.UTC), LocalDateTime.now().toEpochSecond(ZoneOffset.UTC),MINUTE_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE)}"}
            doLogging{"xxooxx \t\t ${DateUtils.getRelativeTimeSpanString(then.toEpochSecond(ZoneOffset.UTC), LocalDateTime.now().toEpochSecond(ZoneOffset.UTC),HOUR_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE)}"}
            doLogging{"xxooxx \t\t ${DateUtils.getRelativeTimeSpanString(then.toEpochSecond(ZoneOffset.UTC), LocalDateTime.now().toEpochSecond(ZoneOffset.UTC),DAY_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE)}"}
            doLogging{"xxooxx \t\t ${DateUtils.getRelativeTimeSpanString(then.toEpochSecond(ZoneOffset.UTC), LocalDateTime.now().toEpochSecond(ZoneOffset.UTC),WEEK_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE)}"}

for example

2023-01-11 11:38:08.143 D  xxooxx past date = 2023-01-11T08:33:18.047
2023-01-11 11:38:08.162 D  xxooxx 11 sec. ago
2023-01-11 11:38:08.163 D  xxooxx    0 min. ago
2023-01-11 11:38:08.164 D  xxooxx    0 hr. ago
2023-01-11 11:38:08.165 D  xxooxx    Today
2023-01-11 11:38:08.165 D  xxooxx    0 wk. ago

2023-01-11T08:33:18.047 is Today, however it is not 11 seconds ago.

2023-01-11 11:38:08.557   xxooxx past date = 2021-11-04T00:00:00
2023-01-11 11:38:08.557   xxooxx 10 hr. ago
2023-01-11 11:38:08.557   xxooxx    10 hr. ago
2023-01-11 11:38:08.558   xxooxx    10 hr. ago
2023-01-11 11:38:08.558   xxooxx    Yesterday
2023-01-11 11:38:08.558   xxooxx    0 wk. ago

2021-11-04 is not 10 hours ago from 2023-01-11 11:38:08.557

Where am I going wrong?

Can I achieve the desired result using android.text.format.DateUtils

CodePudding user response:

First convert the String date to LocalDateTime object.(have used LocalDateTime as date deosn't seems to contain any time zone info. If time zone is also to be considered use ZonedDateTime )

val date1 = "2022-12-22T11:17:43"
val formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME
val dateTime: LocalDateTime = LocalDateTime.parse(date1, formatter)
// current date
val currentDate = LocalDateTime.now()

Then use Duration class to find difference betwwen 2 dates.

  val duration: Duration = Duration.between(currentDate, dateTime)
  val diff: Long = Math.abs(duration.toDays())
  Log.e("Date", "Difference $diff")

Output:

E/Date: Difference 20

Or

Use getRelativeTimeSpanString

val differ = DateUtils.getRelativeTimeSpanString(
dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(),
        Instant.now().toEpochMilli(),
        DateUtils.DAY_IN_MILLIS,
        DateUtils.FORMAT_ABBREV_RELATIVE
    )
    Log.e("Date", "Difference $differ")

Output:

Difference 5 days ago

  • Related