getting time difference with string like "A minute ago" or "An hour ago" on Android -
i have code in php
function nicetime($date) { date_default_timezone_set("asia/taipei"); if(empty($date)) { return "no date provided"; } $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); $lengths = array("60","60","24","7","4.35","12","10"); $now = time(); $unix_date = strtotime($date); // check validity of date if(empty($unix_date)) { return "bad date"; } // future date or past date if($now > $unix_date) { $difference = $now - $unix_date; $tense = "ago"; } else { $difference = $unix_date - $now; $tense = "from now"; } for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) { $difference /= $lengths[$j]; } $difference = round($difference); if($difference != 1) { $periods[$j].= "s"; } return "$difference $periods[$j] {$tense}"; }
but want same time in android. im having trouble because string mytime database, in sql format.
string mytime = pref.getstring("announcementtime" + count, null);
output of mytime:
2013-08-31 15:55:22
i want convert to:
23 minutes ago //something
please aware of default timezone , datetime = now
its in dateutils class.
charsequence getrelativetimespanstring (long time, long now, long minresolution);
gives difference between time , in format like:
54 seconds ago.
to use date string have convert empoch time first:
string mytime = pref.getstring("announcementtime" + count, null); // comes out 2013-08-31 15:55:22 adjust date format simpledateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss"); date date = df.parse(str); long epoch = date.gettime(); string timepassedstring = getrelativetimespanstring (epoch, system.currenttimemillis(), dateutils.second_in_millis);
Comments
Post a Comment