在Android上格式化相对于现在的日期的最佳方法 [英] Best way to format a date relative to now on Android
问题描述
我正在Android应用程序中创建功能,以获取任意日期(过去,现在或将来)并找到相对于现在的差异。
I am creating a feature in an Android app to get an arbitrary date (past, present or future) and find the difference relative to now.
我的 now
和 due
变量是 longs
,这是我的代码:
Both my now
and due
variables are longs
, and this is my code:
long now = System.currentTimeMillis();
long due = now + 864000;
Log.d("Time in 1 day", DateUtils.getRelativeTimeSpanString(due,now, DateUtils.DAY_IN_MILLIS));
我希望输出的内容类似于昨天
,今天
,4天
或 2012/12/19
。但是,当前输出在0天内返回
...
I want the output to be something like yesterday
, today
, in 4 days
or 19/12/2012
. However, the current output returns in 0 days
...
我不希望时间出现在这些日期字符串上。
I don't want the time to appear on these date strings.
我做错了什么,并且是在Android上格式化日期的最佳方法?
What am I doing wrong and is the best method for formatting dates on Android?
推荐答案
我正在改变的想法:
DateUtils.getRelativeTimeSpanString(到期,现在是0L,DateUtils.FORMAT_ABBREV_ALL);
因为文档说它返回了相对于现在的时间。
Since the documentation says it returns the time relative to now.
如果失败,请使用一些出色的库:
If that fails use some of the brilliant libraries:
这篇关于在Android上格式化相对于现在的日期的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!