在Android上获得与“A minute ago”或“An hour ago”等string的时差

我有这个代码在我的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"; } // is it 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}"; } 

但现在我想要做同样的事情,但这次在我的Android。 即时通讯有麻烦,因为stringmytime是从数据库中,在SQL格式。

 String mytime = pref.getString("announcementtime" + count, null); 

mytime的输出:

 2013-08-31 15:55:22 

我想将其转换为:

 23 minutes ago //something like this 

请注意默认的时区和date时间=现在

Solutions Collecting From Web of "在Android上获得与“A minute ago”或“An hour ago”等string的时差"

它全部在DateUtils类中。

 CharSequence getRelativeTimeSpanString (long time, long now, long minResolution); 

给你时间和现在之间的区别,格式如下:

54秒前。

要使用你的datestring,你必须先把它转换成empoch时间:

 String mytime = pref.getString("announcementtime" + count, null); // it comes out like this 2013-08-31 15:55:22 so adjust the 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); 

你可以修改inputvariables“timeAtMiliseconds”,我的例子是在date的格式是在毫秒。

  public static String parseDate(String timeAtMiliseconds) { if (timeAtMiliseconds.equalsIgnoreCase("")) { return ""; } //API.log("Day Ago "+dayago); String result = "now"; SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String todayDate = formatter.format(new Date()); Calendar calendar = Calendar.getInstance(); long dayagolong = Long.valueOf(timeAtMiliseconds) * 1000; calendar.setTimeInMillis(dayagolong); String agoformater = formatter.format(calendar.getTime()); Date CurrentDate = null; Date CreateDate = null; try { CurrentDate = formatter.parse(todayDate); CreateDate = formatter.parse(agoformater); long different = Math.abs(CurrentDate.getTime() - CreateDate.getTime()); long secondsInMilli = 1000; long minutesInMilli = secondsInMilli * 60; long hoursInMilli = minutesInMilli * 60; long daysInMilli = hoursInMilli * 24; long elapsedDays = different / daysInMilli; different = different % daysInMilli; long elapsedHours = different / hoursInMilli; different = different % hoursInMilli; long elapsedMinutes = different / minutesInMilli; different = different % minutesInMilli; long elapsedSeconds = different / secondsInMilli; different = different % secondsInMilli; if (elapsedDays == 0) { if (elapsedHours == 0) { if (elapsedMinutes == 0) { if (elapsedSeconds < 0) { return "0" + " s"; } else { if (elapsedDays > 0 && elapsedSeconds < 59) { return "now"; } } } else { return String.valueOf(elapsedMinutes) + "m ago"; } } else { return String.valueOf(elapsedHours) + "h ago"; } } else { if (elapsedDays <= 29) { return String.valueOf(elapsedDays) + "d ago"; } if (elapsedDays > 29 && elapsedDays <= 58) { return "1Mth ago"; } if (elapsedDays > 58 && elapsedDays <= 87) { return "2Mth ago"; } if (elapsedDays > 87 && elapsedDays <= 116) { return "3Mth ago"; } if (elapsedDays > 116 && elapsedDays <= 145) { return "4Mth ago"; } if (elapsedDays > 145 && elapsedDays <= 174) { return "5Mth ago"; } if (elapsedDays > 174 && elapsedDays <= 203) { return "6Mth ago"; } if (elapsedDays > 203 && elapsedDays <= 232) { return "7Mth ago"; } if (elapsedDays > 232 && elapsedDays <= 261) { return "8Mth ago"; } if (elapsedDays > 261 && elapsedDays <= 290) { return "9Mth ago"; } if (elapsedDays > 290 && elapsedDays <= 319) { return "10Mth ago"; } if (elapsedDays > 319 && elapsedDays <= 348) { return "11Mth ago"; } if (elapsedDays > 348 && elapsedDays <= 360) { return "12Mth ago"; } if (elapsedDays > 360 && elapsedDays <= 720) { return "1 year ago"; } if (elapsedDays > 720) { SimpleDateFormat formatterYear = new SimpleDateFormat("MM/dd/yyyy"); Calendar calendarYear = Calendar.getInstance(); calendarYear.setTimeInMillis(dayagolong); return formatterYear.format(calendarYear.getTime()) + ""; } } } catch (java.text.ParseException e) { e.printStackTrace(); } return result; } 

在Github上有一个很好的库: https : //github.com/curioustechizen/android-ago