0

I'm using this code to get relative date time in android

    long now = System.currentTimeMillis();
    long difference = now - this.mReferenceTime;
    if ((0 <= difference) && (DateUtils.MINUTE_IN_MILLIS >= difference)) {
        return getResources().getString(R.string.just_now);
    } else {
        return DateUtils.getRelativeTimeSpanString(
                this.mReferenceTime,
                now,
                DateUtils.MINUTE_IN_MILLIS,
                DateUtils.FORMAT_ABBREV_MONTH | DateUtils.FORMAT_ABBREV_RELATIVE).toString();

    }

The output is coming as "28 Mar". How do I make this to "Mar 28"?

This will return strings like "Just now" or "2 minutes ago" or "6 days ago" or something like "28 Mar". I just wanna change the "28 Mar" part to "Mar 28".

Blaze Mathew
  • 185
  • 9
  • Check out : http://stackoverflow.com/questions/7082518/android-getrelativetime-example – Haresh Chhelana Jun 05 '15 at 08:53
  • so whats the answer? – Blaze Mathew Jun 05 '15 at 08:58
  • you can split and swap the output string – Shekhar Khairnar Jun 05 '15 at 09:00
  • so split and swap? i thought there will be some DateUtils.FORMAT_.... – Blaze Mathew Jun 05 '15 at 09:03
  • 1
    Can you show what's mReferenceTime and now values? Because i'm testing your code, and it gives me __Jun 14__ with my params – Nik Myers Jun 05 '15 at 09:21
  • Why do you set the flag `DateUtils.FORMAT_ABBREV_MONTH` if you want a relative time in minutes? This flag seems to be totally inappropriate because it is only for date representations, not for relative times expressing durations. Please clarify. – Meno Hochschild Jun 05 '15 at 09:39
  • According to [javadoc](http://developer.android.com/reference/android/text/format/DateUtils.html): `Can use FORMAT_ABBREV_RELATIVE flag to use abbreviated relative times, like "42 mins ago".` – Meno Hochschild Jun 05 '15 at 09:41
  • @NikMyers so could it be a phone settings thing then? – Blaze Mathew Jun 05 '15 at 09:56
  • added full code + explanation – Blaze Mathew Jun 05 '15 at 10:04
  • I still don't understand. How is "28 Mar" (or "Mar 28") a relative time? Relative to what? It rather looks like a date without year, and that format has no reference to now, but the notation of a relative time like "2 mins ago" is just another term for a duration relative to now. You are not supposed to call `getRelativeTimespanString(...)` with a dateformat-flag like `FORMAT_ABBREV_MONTH`. An example for the horrible and unsafe design of helper class `DateUtils`. – Meno Hochschild Jun 05 '15 at 10:12
  • It has some limit on how many __days ago__ could be, if difference is larger, that it just showing MONTH DAY, you didn't add your values for __now__ and __ReferenceTime__, i need them to test, just paste long values please – Nik Myers Jun 05 '15 at 10:15

2 Answers2

0

For this output you can use SimpleDateFormat like this;

SimpleDateFormat sdf = new SimpleDateFormat("MMM dd");
String currentDateTime = sdf.format(new Date());

Good luck.

Batuhan Coşkun
  • 2,961
  • 2
  • 31
  • 48
0

change your format like this

SimpleDateFormat srcDf = new SimpleDateFormat(
                            "yyyy.dd.MM HH:mm:ss");

you can change your format more ways - http://developer.android.com/reference/java/text/SimpleDateFormat.html

Saket Mittal
  • 3,726
  • 3
  • 29
  • 49