自定义android日期,如twitter和instagram新闻源

时间:2015-07-06 23:08:21

标签: java android simpledateformat android-dateutils

如何在Android开发中自定义日期格式,就像twitter和instagram一样。我目前的代码下面有什么,但我不喜欢它产生的格式,如同#11; 11分钟前"或者" 34分钟前"。我更喜欢推特格式,如" 11m"或者" 34m"。请有人知道我如何格式化我的日期吗?

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

1 个答案:

答案 0 :(得分:4)

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

以下是我制作的实用程序方法(有用的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.

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