4

如何在 android 开发中自定义日期格式,使其类似于 twitter 和 instagram。我在当前代码下面有什么,但我不喜欢它产生的格式,如“11 分钟前”或“34 分钟前”。我更喜欢“11m”或“34m”之类的 twitter 格式。请问有谁知道我可以这样格式化我的日期吗?

Date createdAt = message.getCreatedAt();//get the date the message was created from parse backend
        long now = new Date().getTime();//get current date
        String convertedDate = DateUtils.getRelativeTimeSpanString(
                createdAt.getTime(), now, DateUtils.SECOND_IN_MILLIS).toString();
        mPostMessageTimeLabel.setText(convertedDate); //sets the converted date into the message_item.xml view
4

2 回答 2

6

有同样的问题。而不是使用库,我想我可能可以编写自己的版本,并让它对正在发生的事情更容易理解(如果需要,可以稍微调整一下)。

这是我制作的实用方法(Log包括对 Android 用户进行测试的有用语句):

public static String convertLongDateToAgoString (Long createdDate, Long timeNow){
        Long timeElapsed = timeNow - createdDate;

        // For logging in Android for testing purposes
        /*
        Date dateCreatedFriendly = new Date(createdDate);
        Log.d("MicroR", "dateCreatedFriendly: " + dateCreatedFriendly.toString());
        Log.d("MicroR", "timeNow: " + timeNow.toString());
        Log.d("MicroR", "timeElapsed: " + timeElapsed.toString());*/

        // Lengths of respective time durations in Long format.
        Long oneMin = 60000L;
        Long oneHour = 3600000L;
        Long oneDay = 86400000L;
        Long oneWeek = 604800000L;

        String finalString = "0sec";
        String unit;

        if (timeElapsed < oneMin){
            // Convert milliseconds to seconds.
            double seconds = (double) ((timeElapsed / 1000));
            // Round up
            seconds = Math.round(seconds);
            // Generate the friendly unit of the ago time
            if (seconds == 1) {
                unit = "sec";
            } else {
                unit = "secs";
            }
            finalString = String.format("%.0f", seconds) + unit;
        } else if (timeElapsed < oneHour) {
            double minutes = (double) ((timeElapsed / 1000) / 60);
            minutes = Math.round(minutes);
            if (minutes == 1) {
                unit = "min";
            } else {
                unit = "mins";
            }
            finalString = String.format("%.0f", minutes) + unit;
        } else if (timeElapsed < oneDay) {
            double hours   = (double) ((timeElapsed / 1000) / 60 / 60);
            hours = Math.round(hours);
            if (hours == 1) {
                unit = "hr";
            } else {
                unit = "hrs";
            }
            finalString = String.format("%.0f", hours) + unit;
        } else if (timeElapsed < oneWeek) {
            double days   = (double) ((timeElapsed / 1000) / 60 / 60 / 24);
            days = Math.round(days);
            if (days == 1) {
                unit = "day";
            } else {
                unit = "days";
            }
            finalString = String.format("%.0f", days) + unit;
        } else if (timeElapsed > oneWeek) {
            double weeks = (double) ((timeElapsed / 1000) / 60 / 60 / 24 / 7);
            weeks = Math.round(weeks);
            if (weeks == 1) {
                unit = "week";
            } else {
                unit = "weeks";
            }
            finalString = String.format("%.0f", weeks) + unit;
        }
        return finalString;
    }

用法:

Long createdDate = 1453394736888L; // Your Long
Long timeNow = new Date().getTime();
Log.d("MicroR", convertLongDateToAgoString(createdDate, timeNow));

// Outputs:
// 1min
// 3weeks
// 5hrs
// etc.

随意测试一下,如果您发现任何问题,请告诉我!

于 2016-02-17T12:00:44.123 回答
1

我可能有点晚了,但我把它写下来给正在寻找解决方案的人。使用PrettyTime,您可以获得“2 个月前”等格式化日期。为了满足您的需求,您必须为其提供自定义对象,因为我们正在格式化正常时间单位TimeFormat,因此无需创建新对象。TimeUnit为此,只需花TimeFormat几分钟时间创建您的对象,例如:

public class CustomMinuteTimeFormat implements TimeFormat {
@Override
public String format(Duration duration) {
    return Math.abs(duration.getQuantity()) + "m";
}

@Override
public String formatUnrounded(Duration duration) {
    return format(duration);
}

@Override
public String decorate(Duration duration, String time) {
    return time;
}

@Override
public String decorateUnrounded(Duration duration, String time) {
    return time;
}
}

然后实例化一个新PrettyTime实例并设置您的格式化程序。

PrettyTime pretty = new PrettyTime();
//This line of code is very important
pretty.registerUnit(new Minute(), new CustomMinuteTimeFormat());
//Use your PrettyTime object as usual
pretty.format(yourDateObject);

如果经过的时间为 2 分钟,这将输出“2m”。

于 2015-09-09T16:30:56.890 回答