【发布时间】:2014-12-18 15:15:30
【问题描述】:
考虑到 ISO8601 在 JSON 中的使用频率,我还没有找到一种真正简单的方法,这让我感到非常惊讶。
基本上,我会采用如下所示的字符串:2014-10-23T00:35:14.800Z 并将其转换为类似50 minutes ago 的字符串。
首先我要把2014-10-23T00:35:14.800Z改成2014-10-23'T'00:35:14.800Z,然后再转换成毫秒,就很简单了。
我当前的代码:
private void setTimestamp(String timeCreated) {
int indexOfT = timeCreated.indexOf('T');
String properFormat = new StringBuilder(timeCreated).insert(indexOfT + 1, "'")
.insert(indexOfT, "'")
.toString();
timeStamp = (String) DateUtils.getRelativeTimeSpanString(Long.parseLong(properFormat),
System.currentTimeMillis(),
DateUtils.SECONDS_IN_MILLIS);
}
罪魁祸首是Long.parseLong(properFormat)。我需要将properFormat 转换为毫秒。
【问题讨论】:
-
如果您使用 Java 8,您可能会发现 this discussion 很有趣。
-
请告诉我你为什么不赞成这个。
-
@David 我不是反对者。但我怀疑投票否决可能是由于 StackOverflow 上数百个问题和答案处理了这个主题。
-
@PM77-1 Android 中没有 Java 8 和 java.time。