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.
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”.
Would I use the Calendar object, SimpleDateFormat, something else?
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.
EDIT: I use this:
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 settingMILLIS_PER_YEARthis way:28is for February, in this case you only be 1 day off on leaps years, instead of 5 days as in former version.