4

I want to translate a date to human readable format. I am using DateUtils. getRelativeDateTimeString, but this does not fit the criteria. The output I am getting looks like: 1 hour, 15 min. ago, etc.

I want to know if it is possible to change the format to:

3m instead of 3 min. ago,

1h instead of 1 hour. 15 min. ago etc.

using DateUtils or is there another way to do this?

To be more precise I am looking for an Android equivalent of this angular-filter where you can easily change the format of the relative date (for example: {{minutes}} minutes ago to {{minutes}}m.

To make myself clear, I am not searching for a way to format a date, but to translate a date to human readable format such as 'today', '1 hr', '38 min' (simillar to facebook's relative dates).

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
Monika Bozhinova
  • 294
  • 3
  • 16
  • My library [Time4A](https://github.com/MenoData/Time4A) offers the class [PrettyTime](http://time4j.net/javadoc-en/net/time4j/PrettyTime.html) for about 85 languages and different text widths. – Meno Hochschild Feb 15 '17 at 15:54

4 Answers4

2

After some research, I found out some libraries like Time4A, Joda-Time, PrettyTime, Android-Ago.

However, I have decided not to use a library and override its text resources, but to create a method and store text in strings.xml for possible future localization.

    private static final int SECOND_MILLIS = 1000;
    private static final int MINUTE_MILLIS = 60 * SECOND_MILLIS;
    private static final int HOUR_MILLIS = 60 * MINUTE_MILLIS;
    private static final int DAY_MILLIS = 24 * HOUR_MILLIS;
    private static final int WEEK_MILLIS = 7 * DAY_MILLIS;

    public static String getTimeAgo(Date date, Context context) {
        Date now = Calendar.getInstance().getTime();
        final long diff = now.getTime() - date.getTime();

        if (diff < SECOND_MILLIS) {
            return context.getString(R.string.just_now);
        } else if (diff < MINUTE_MILLIS) {
            return diff / SECOND_MILLIS + context.getString(R.string.seconds_ago);
        } else if (diff < 2 * MINUTE_MILLIS) {
            return context.getString(R.string.a_minute_ago);
        } else if (diff < 59 * MINUTE_MILLIS) {
            return diff / MINUTE_MILLIS + context.getString(R.string.minutes_ago);
        } else if (diff < 90 * MINUTE_MILLIS) {
            return context.getString(R.string.an_hour_ago);
        } else if (diff < 24 * HOUR_MILLIS) {
            return diff / HOUR_MILLIS + context.getString(R.string.hours_ago);
        } else if (diff < 48 * HOUR_MILLIS) {
            return context.getString(R.string.yesterday);
        } else if (diff < 6 * DAY_MILLIS) {
            return diff / DAY_MILLIS + context.getString(R.string.days_ago);
        } else if (diff < 11 * DAY_MILLIS) {
            return context.getString(R.string.a_week_ago);
        } else {
            return diff / WEEK_MILLIS + context.getString(R.string.weeks_ago);
        }
    }
Monika Bozhinova
  • 294
  • 3
  • 16
-1

Use the built-in DateUtils utility library that was included in API level 3.

 CharSequence getRelativeDateTimeString (Context c, 
             long time, 
             long minResolution, 
             long transitionResolution, 
             int flags) Return string describing the elapsed time since startTime formatted like "[relative time/date], [time]".

Example output strings for the US date format:

3 min. ago, 10:15 AM Yesterday, 12:20 PM Dec 12, 4:12 AM 11/14/2007, 8:20 AM

goto
  • 7,908
  • 10
  • 48
  • 58
Juuso
  • 487
  • 5
  • 12
-2

Why don't you parse the Date to the format you want?

With string.replace() you can do it in one line of code.

Edit 1: I usually use SimpleDateForma this way, hope it helps:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String currentDateandTime = sdf.format(new Date());

you will need this imports ( i think that Android automatically imports):

import java.text.SimpleDateFormat;
import java.util.Date;

Edit 2: In your example what you have to do is:

SimpleDateFormat sdf = new SimpleDateFormat("HH'h' mm'm'"); String currentDateandTime = sdf.format(new Date()); System.out.println("Today in dd-MM-yy:HH:mm:SS : " + date);

Tomás Rodrigues
  • 519
  • 2
  • 17
  • 1
    How would you parse Date object into 3 minutes ago or 1 hour ago? It's not one line of code. I am asking if there is some library or util for this. – Monika Bozhinova Feb 15 '17 at 15:53
  • 1
    The OP does not want to format an object of type `java.util.Date` but the delta between two objects of this type i.e. a duration. That is a big difference. – Meno Hochschild Feb 15 '17 at 16:00
  • Meno Hochschild, exactly! It's called relative date or relative time. – Monika Bozhinova Feb 15 '17 at 16:05
  • Two facts about your solution suggested in "edit-2": `SimpleDateFormat` is not designed or suitable for printing durations => a) cannot handle field overflow (example: 25 hours), b) suffers from strange timezone effects. – Meno Hochschild Feb 15 '17 at 16:25
-2

You should use SimpleDateFormat and specify your desired template. Which in your case would be something like this:

String template = "H'h', m'm'";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(template, Locale.getDefault());
String formatted = simpleDateFormat.format(new Date());
cpienovi
  • 410
  • 4
  • 7