Search code examples
javaandroidandroid-dateutils

How to increase elapsed time for getRelativeDateTimeString method, also how to month name for normal date


I have used getRelativeDateTimeString which is showing the maximum of 1 week elapsed time.

  1. How to increase more than 1 week.

  2. after the elapsed time it shows the normal date formatting like 11/14/2007, 8:20 AM, I want to show month name in a date like Nov/14/2007, 8:20 AM or 14 Nov 2007, 8:20 AM

I expect output to be: Nov/14/2007, 8:20 AM but the output is 11/14/2007, 8:20 AM

java.util.Date time = new java.util.Date((long) time1 * 1000);
    int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR | FORMAT_ABBREV_MONTH;
    CharSequence mdate = DateUtils.getRelativeDateTimeString(context,time.getTime() , DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, flags);

Solution

  • You can break it into two parts:

    1. get the date in your desired format using DateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.mmm'Z'");
    2. get the time elapse using getRelativeTimeSpanString

    hope it helps, thanks