我在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中。 我遇到了麻烦,因为字符串mytime来自数据库,采用SQL格式。
String mytime = pref.getString("announcementtime" + count, null);
mytime的输出:
2013-08-31 15:55:22
我想将其转换为:
23 minutes ago //something like this
请注意默认时区和DateTime = Now
答案 0 :(得分:29)
全部都在DateUtils级。
CharSequence getRelativeTimeSpanString (long time, long now, long minResolution);
以下列格式为您提供时间与现在的区别:
54秒前。
要使用日期字符串,您必须先将其转换为时间字符:
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);
答案 1 :(得分:5)
您可以修改输入变量&#34; timeAtMiliseconds&#34;,对于我的示例,日期格式为毫秒。
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;
}
答案 2 :(得分:2)
在Github上有一个很好的图书馆用于此目的: https://github.com/curioustechizen/android-ago