Android中的ListView自定义日期(Java的格式化) [英] Custom Dates in Android ListView (Java Formatting)

查看:328
本文介绍了Android中的ListView自定义日期(Java的格式化)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView 用户将会把意见。当他们点击提交附加到的EditText 中,新注释进入一个的MySQL 数据库以及当前时间。

I have a ListView that users will put comment in. When they hit "submit" attached to an EditText box, the new comment goes into a MySQL database along with the current time.

我想的评论出现在同日期类似YouTube格式列表视图。例如:10秒前,1天前,在49天前

I'd like the comments to appear in the listview with dates similar to Youtube formatting. For example: "10 seconds ago", "1 day ago", "52 days ago".

我会使用Calendar对象,SimpleDateFormat的,别的东西?

Would I use the Calendar object, SimpleDateFormat, something else?

另外,我怎么会存储在DATEBASE的日期?我假设的东西易于转换类似于UNIX的日期戳可能。然后会是在一个ArrayAdapter,我根据当前时间动态变化的日期?

Also, how would I store the date in the datebase? I am assuming something easily convertible like the UNIX date stamp maybe. And then would it be in the ArrayAdapter where I dynamically change the date based on the current time?

推荐答案

要得到这一点,你需要计算你的时间戳和当前时间戳之间的差值(保持他们以毫秒为简单起见)。其结果将是前的毫秒数。然后用简单的数学像减法和除法,你可以得到几分钟,几天,几周和任何你想要的号码。

To get this you need to calculate the difference between your timestamp and current timestamp (keep them in milliseconds for simplicity). The result will be number of milliseconds "ago". Then use simple math like subtraction and division and you can get number of minutes, days, weeks and whatever-you-want.

编辑:我用这个:

public static final long MILLIS_PER_SECOND  = 1000;
public static final long MILLIS_PER_MINUTE  = (MILLIS_PER_SECOND * 60);
public static final long MILLIS_PER_HOUR    = (MILLIS_PER_MINUTE * 60);
public static final long MILLIS_PER_DAY     = (MILLIS_PER_HOUR * 24);
public static final long MILLIS_PER_WEEK    = (MILLIS_PER_DAY * 7);
public static final long MILLIS_PER_MONTH   = (MILLIS_PER_DAY * 30);
public static final long MILLIS_PER_YEAR    = (MILLIS_PER_MONTH * 12);

请注意这是不是100%正确的,因为我(特意为simplicy)提出的假设一个月始终为30天之久,这也影响 MILLIS_PER_YEAR 。但我真的不关心 - 这不是火箭科学我使用这些的。但是你可以通过设置减少影响 MILLIS_PER_YEAR 是这样的:

Note that is is not 100% correct as I (intentionally, for simplicy) made false assumption month is always 30 days long, which also influences MILLIS_PER_YEAR. But I do not really care - it's not rocket science I use these for. But you may reduce the impact by setting MILLIS_PER_YEAR this way:

public static final long MILLIS_PER_YEAR = (MILLIS_PER_DAY * (7*31 + 4*30 + 28));

28 是2月,在这种情况下,你只能为1一天的跨越年,而不是5天为在前的版本。

28 is for February, in this case you only be 1 day off on leaps years, instead of 5 days as in former version.

这篇关于Android中的ListView自定义日期(Java的格式化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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