4

I have the following code to convert a server returned date string to a since string.

/**
* Change date format to "since" string
* */
public static String timeSince(String dateString) {
    Date date = stringToDate(dateString);
    String result = (DateUtils.getRelativeTimeSpanString(date.getTime())).toString();
    return result;
}
/**
 * Function to convert server date string to Date
 * */
public static Date stringToDate(String s){
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    try {
        return df.parse(s);
    } catch(ParseException e){
        e.printStackTrace();
    }
    return null;
}

But, as an example, if I call timeSince("2016-07-04T07:21:39.575Z") I get "Jul 4, 2016" as a result, instead of something like "3 days ago" or any other period relative to now time. Any idea why ? Thx...

DrMad
  • 490
  • 3
  • 12
  • what happens if you use SimpleDateFormat("yyyy-MM-dd") ? – Mohammadreza Khatami Jul 13 '16 at 05:17
  • are you sure that this is the code that is being called? – Scary Wombat Jul 13 '16 at 05:21
  • @MohammadRezaKhatami Same thing happens – DrMad Jul 13 '16 at 05:24
  • @ScaryWombat Of course, I'm sure. I was debugging line by line and got the mentioned results – DrMad Jul 13 '16 at 05:25
  • It's not a String, it's a CharSequence – DrMad Jul 13 '16 at 05:34
  • as per the Javadocs `a relative time string to display the time expressed by millis. Times are counted starting at midnight, which means that assuming that the current time is March 31st, 0:30: "millis=0:10 today" will be displayed as "0:10" "millis=11:30pm the day before" will be displayed as "Mar 30" If the given millis is in a different year, then the full date is returned in numeric format (e.g., "10/12/2008").` – Scary Wombat Jul 13 '16 at 05:39
  • Please refer to this answer - http://stackoverflow.com/questions/13018550/time-since-ago-library-for-android-java - the snippet from google IO – galvan Jul 13 '16 at 05:50
  • again, that does not answer my question: "why do I get an absolute date instead of a "since period" ? – DrMad Jul 13 '16 at 06:12

2 Answers2

1

OK. It turns out that DateUtils.getRelativeTimeSpanString(date.getTime()) returns a RELATIVE duration (e.g."yesterday" or "30 minutes ago") EXCEPT if that duration is greater than a week, in which case it returns an ABSOLUTE (look at the code) date...

Nothing in the documentation says so... But that's a fact. Another drawback to most of the Android solutions is that the messages are not localized ("3 minutes ago" does not work in French nor Spanish nor any other language for that matter). So I will probably end up writing my own library for this.

Bottom line is that if you use English and want to display the date as an absolute date if it is more than a week ago, the above code works.

DrMad
  • 490
  • 3
  • 12
-1

Try this :

Date now = new Date();
String str = DateUtils.getRelativeDateTimeString(

     this, // Suppose you are in an activity or other Context subclass

     now.getTime(), // The time to display

     DateTimeUtils.MINUTE_IN_MILLIS, // The resolution. This will display only 
                                        // minutes (no "3 seconds ago") 


     DateTimeUtils.WEEK_IN_MILLIS, // The maximum resolution at which the time will switch 
                         // to default date instead of spans. This will not 
                         // display "3 weeks ago" but a full date instead

        0); // Eventual flags

Other values for MINUTE_IN_MILLIS and YEAR_IN_MILLIS include:

  • SECOND_IN_MILLIS
  • MINUTE_IN_MILLIS
  • HOUR_IN_MILLIS
  • DAY_IN_MILLIS
  • WEEK_IN_MILLIS
  • YEAR_IN_MILLIS

Any custom value in milliseconds

DateUtils.getRelativeTimeSpanString(yourContext, theEventInMillis, 
                                     DateUtils.MINUTE_IN_MILLIS, 
                                     DateUtils.FORMAT_NO_NOON);

This will format theEventInMillis relative to whatever the system's current time is. It will show changes in minutes (0 minutes ago, 2 minutes ago, 3 hours ago, 1 day ago, etc) until the difference reaches a week, then it will just post the full date.

I hope this helps you.

Mohammadreza Khatami
  • 1,444
  • 2
  • 13
  • 27