Custom Dates In Android Listview (java Formatting)
Solution 1:
You can use PrettyTime to achieve that.
For saving time, I usually save time in millis.
Solution 2:
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:
publicstaticfinallong MILLIS_PER_SECOND = 1000;
publicstaticfinallong MILLIS_PER_MINUTE = (MILLIS_PER_SECOND * 60);
publicstaticfinallong MILLIS_PER_HOUR = (MILLIS_PER_MINUTE * 60);
publicstaticfinallong MILLIS_PER_DAY = (MILLIS_PER_HOUR * 24);
publicstaticfinallong MILLIS_PER_WEEK = (MILLIS_PER_DAY * 7);
publicstaticfinallong MILLIS_PER_MONTH = (MILLIS_PER_DAY * 30);
publicstaticfinallong MILLIS_PER_YEAR = (MILLIS_PER_MONTH * 12);
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:
publicstaticfinallong MILLIS_PER_YEAR = (MILLIS_PER_DAY * (7*31 + 4*30 + 28));
28
is for February, in this case you only be 1 day off on leaps years, instead of 5 days as in former version.
Solution 3:
Try DateUtils.getRelativeTimeSpanString to format the date for display.
Post a Comment for "Custom Dates In Android Listview (java Formatting)"