Set date android date as twitter and instagram news feed

how can I customize the date format in Android development like on twitter and instagram. Which is lower than my current code, but I don’t like the format it produces, for example, “11 minutes ago” or “34 minutes ago”. I prefer the twitter format as “11 meters” or “34 meters”. Please, does anyone know how I can format my date?

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
source share
2 answers

, , , . 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 ", 2 .

+1

. , , , , (, , ).

, ( 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.

, - !

+1

All Articles