2

I am wondering how to properly display dates in Android. I mean date-formats, which fit to the specific region, where the application is downloaded. What I am doing atm is:

DateFormat formatter = DateFormat.getDateTimeInstance();
String[] dateParts = date.split(DATE_DELIMITER);
Date temp = new Date(Integer.valueOf(dateParts[4])-1900, Integer.valueOf(dateParts[3]),
            Integer.valueOf(dateParts[2]), Integer.valueOf(dateParts[0]),
            Integer.valueOf(dateParts[1]));
return formatter.format(temp);

So... There are following problems with this: Java.util.Date is marked as deprecated, and it displays me seconds too, although I don't want that.

Is there any "Beautiful" solution for that? I'd be glad if you could tell me :)

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Aeefire
  • 888
  • 10
  • 25
  • to all answers yet: What I don't want is a fixxed position of "date and month" as different regions handle this differently. E.g. europe is used to DD.MM. UK or US is sometimes used to MM/DD and so on. Is there no way to do this? – Aeefire Feb 03 '13 at 11:03

2 Answers2

5

EDIT

To format a date for the current Locale, use one of the static factory methods:

String formattedString = DateFormat.getDateInstance().format(yourDate);

This will format accordingly to the device current locale, that is the one configured in android settings.

More Info


A solution is to use a SimpleDateFormat

public static vod FormatDate(Date yourDate) {

    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

    return sdf.format( yourDate ));

}

Related: Android SimpleDateFormat, how to use it?

Community
  • 1
  • 1
RMalke
  • 4,048
  • 29
  • 42
2

Android DateFormat Using Device Current Locale

In android you can use the device's locale while formatting your date object by doing this:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss", Locale.getDefault())
String formatedText = sdf.format(new Date());

Edit

To use the exact system Date Formates use:

        DateFormat.getDateFormat(mContext).format(new Date());
Mr.Me
  • 9,192
  • 5
  • 39
  • 51