如何将时间转换为“时间之前在Android中 [英] How to convert time to " time ago " in android

查看:122
本文介绍了如何将时间转换为“时间之前在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器.返回时间:

My server. It return time :

"2016-01-24T16:00:00.000Z"

我想要

1:转换为字符串.

2:从服务器加载时,我希望它显示"time ago".

2 : I want it show " time ago " when load it from server.

请.救救我!

推荐答案

我主要看到三种方式:

a)使用SimpleDateFormat

b)外部库 ocpsoft/PrettyTime (基于java.util.Date)

在这里,您还必须使用SimpleDateFormat来生成time结果作为对"2016-01-24T16:00:00.000Z"的解释.

Here you have to use SimpleDateFormat, too, to produce the time-result as interpretation of "2016-01-24T16:00:00.000Z".

在应用程序的lib下面导入

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)使用我的库 Time4A (重量级,但具有最好的i18n支持)

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);

这篇关于如何将时间转换为“时间之前在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆