11

我的 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

4

3 回答 3

35

它都在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);
于 2013-09-04T06:50:50.883 回答
6

您可以修改输入变量“timeAtMiliseconds”,因为我的示例是日期格式为毫秒。

  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;
}
于 2015-10-15T14:49:39.070 回答
4

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

于 2013-09-04T06:47:36.987 回答