Java – Android: how to build dates from server dates

I have the following code to convert the date string returned by the server into a self - 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;
}

However, as an example, if I call timesince ("2016-07-04t07:21:39.575z"), I get "July 4, 2016", not "three days ago" or any other period relative to the present Do you know why? Thank you

Solution

OK It turns out that dateutils Getrelativetimespanstring (date. Gettime()) returns a relative duration (e.g. "yesterday" or "30 minutes ago") unless the duration is greater than a week, in which case it returns an approximate date... Nothing in the document says so... But this is true Another disadvantage of most Android solutions is that messages are not localized ("three minutes ago" didn't work in French, Spanish or any other language) So I may eventually write my own library for this Most importantly, 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 is valid

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>