如何将时间转换为“前一段时间"在安卓中 [英] How to convert time to " time ago " in android
问题描述
我的服务器.返回时间:
"2016-01-24T16:00:00.000Z"
我想要
1 : 转换为字符串.
2 : 我希望它在从服务器加载时显示时间前".
请.帮帮我!
我看主要有三种方式:
a) 使用 SimpleDateFormat
和 DateUtils
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");sdf.setTimeZone(TimeZone.getTimeZone("GMT"));尝试 {长时间 = sdf.parse("2016-01-24T16:00:00.000Z").getTime();long now = System.currentTimeMillis();CharSequence 前 =DateUtils.getRelativeTimeSpanString(时间,现在,DateUtils.MINUTE_IN_MILLIS);} catch (ParseException e) {e.printStackTrace();}
b) 外部库 ocpsoft/PrettyTime(基于 java.util.Date代码>)
在这里,您也必须使用 SimpleDateFormat
来生成 time
-result 作为对2016-01-24T16:00:00.000Z"的解释.>
在您的应用中导入下面的 lib
实现'org.ocpsoft.prettytime:prettytime:4.0.1.Final'
PrettyTime PrettyTime = new PrettyTime(Locale.getDefault());String ago = prettyTime.format(new Date(time));
c) 使用我的库 Time4A(重量级但具有最佳 i18n 支持)
Moment moment = Iso8601Format.EXTENDED_DATE_TIME_OFFSET.parse("2016-01-24T16:00:00.000Z");String ago = PrettyTime.of(Locale.getDefault()).printRelativeInStdTimezone(moment);
My server. It return time :
"2016-01-24T16:00:00.000Z"
I want
1 : convert to String.
2 : I want it show " time ago " when load it from server.
Please. Help me!
I see mainly three ways:
a) built-in options using SimpleDateFormat
and DateUtils
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
try {
long time = sdf.parse("2016-01-24T16:00:00.000Z").getTime();
long now = System.currentTimeMillis();
CharSequence ago =
DateUtils.getRelativeTimeSpanString(time, now, DateUtils.MINUTE_IN_MILLIS);
} catch (ParseException e) {
e.printStackTrace();
}
b) external library ocpsoft/PrettyTime (based on java.util.Date
)
Here you have to use SimpleDateFormat
, too, to produce the time
-result as interpretation of "2016-01-24T16:00:00.000Z".
import below lib in your app
implementation 'org.ocpsoft.prettytime:prettytime:4.0.1.Final'
PrettyTime prettyTime = new PrettyTime(Locale.getDefault());
String ago = prettyTime.format(new Date(time));
c) using my library Time4A (heavyweight but with best i18n-support)
Moment moment = Iso8601Format.EXTENDED_DATE_TIME_OFFSET.parse("2016-01-24T16:00:00.000Z");
String ago = PrettyTime.of(Locale.getDefault()).printRelativeInStdTimezone(moment);
这篇关于如何将时间转换为“前一段时间"在安卓中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!