10

Possible Duplicate:
Android get current time and date

From this Format date without year I know how to get rid of year.

But I also want to formate month and day. if user set 09/24/2012, it displays 09/24; if user set 24/09/2012, it displays 24/09.

And I also wanna support l18n.

Maybe the solution is combine DateFormat and DateUtils, but I don't know how to do it.

Community
  • 1
  • 1
Fenjin Wang
  • 331
  • 2
  • 12
  • http://stackoverflow.com/questions/5369682/android-get-current-time-and-date Did you look into this. – Nate-Wilkins Sep 24 '12 at 02:22
  • Maybe that's not I want. Actually I got a long and wanna format it to date and used int TextView.setText(). I found a function DateFormat.getDateFormatOrder(), but I don't know how to use it. – Fenjin Wang Sep 24 '12 at 02:32
  • If the question is on whether to display the M/D over D/M, just do D/M, most countries use D/M/Y – mrres1 Sep 24 '12 at 02:56
  • 2
    Here is my 'locale aware' solution DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.LONG); // use MEDIUM or SHORT according to your needs String date = dateFormatter.format(now); // remove year String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR)); date = date.replace(year, "").trim(); will output "13 décembre" in French :-) – Pascal Dec 13 '12 at 09:28
  • Missing line in my solution: Date now = new Date(); ... of course – Pascal Dec 13 '12 at 09:35
  • 2
    Fun fact: this question is really not a duplicate. Yes, we all know how to get a specifically-formatted date. That's not what this question is asking. It looks like http://stackoverflow.com/questions/3790918/format-date-without-year would actually work, since android.text.format.DateFormat uses SimpleDateFormats. That's still way ugly, though. – pforhan Apr 24 '13 at 14:50

1 Answers1

-2

First split the information into variables, then just display as needed.

public String formatDate(String date)
{
    int index1, index2;

    index1 = date.indexOf('/');
    index2 = date.lastIndexOf('/');

    String str1, str2, str3;

    str1 = date.substring(0, index1);
    str2 = date.substring(index1 + 1, index2);
    str3 = date.substring(index2);

    // Presuming that str1 is the month and str2 is the day ...

    return str1 + "/" + str2;
}
mrres1
  • 1,147
  • 6
  • 10
  • 1
    This will fail miserably on all devices where locale is set to format date in other than american forms (like 24.10.1998 or some such). – Mavrik Mar 06 '13 at 14:04
  • I would assume the individual who would use this code would be intelligent enough to know that. – mrres1 Mar 19 '13 at 11:24