【问题标题】:DateUtils.getRelativeTimeSpanString returns the difference in time but result includes the current timeDateUtils.getRelativeTimeSpanString 返回时间差,但结果包括当前时间
【发布时间】:2019-01-12 21:40:26
【问题描述】:
CharSequence string = DateUtils.getRelativeDateTimeString(getActivity(), System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS,
            DateUtils.DAY_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE);

结果是“0 分钟前,00:19”我只需要“0 分钟前”,我如何摆脱添加到它的当前时间。

【问题讨论】:

  • 我的回答有用还是您还需要帮助?
  • 这让我找回了时间,这里的“00:19”就是我想要摆脱的
  • 哦,你想摆脱它,我的坏

标签: android android-dateutils


【解决方案1】:

你可以这样做:

CharSequence string = (DateUtils.getRelativeDateTimeString(getActivity(), System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS,
        DateUtils.DAY_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE)).subSequence(string.length() - 5, string.length());

【讨论】:

  • 我不是在固定时间而是在不同的时间调用该方法。有时差异可能是“12 分钟前”,这使得字符串长度增加。我不能只返回差值而不添加当前时间,或者我该怎么办?
  • 我已经通过调用函数string.length() 来解释这一点,该函数将动态获取字符串长度。你试过了吗?我还将我的答案更新为一行代码。
【解决方案2】:
string.substring(0, string.indexOf(",") 

解决了

【讨论】:

  • 这实际上是不正确的,因为它会给你一个额外的空间并返回一个字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多