31

Does anybody know how to get the format string used by the system when formatting a date using

DateFormat.getLongDateFormat(Context context).format(Date date)
Ben
  • 51,770
  • 36
  • 127
  • 149
Androrider
  • 1,050
  • 2
  • 13
  • 20
  • Pls choose the other answer as the accepted one. The current one is a hack (as noted also by the person who answered). – milosmns Mar 12 '19 at 14:27

8 Answers8

75

To get the date format pattern you can do:

Format dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
String pattern = ((SimpleDateFormat) dateFormat).toLocalizedPattern();
Mark Melling
  • 1,562
  • 14
  • 15
7

I wrote a method to detect this format string. ( work for my case).

public static String getDateFormat(Context context){
        // 25/12/2013
        Calendar testDate = Calendar.getInstance();
        testDate.set(Calendar.YEAR, 2013);
        testDate.set(Calendar.MONTH, Calendar.DECEMBER);
        testDate.set(Calendar.DAY_OF_MONTH, 25);

        Format format = android.text.format.DateFormat.getDateFormat(context);
        String testDateFormat = format.format(testDate.getTime());
        String[] parts = testDateFormat.split("/");
        StringBuilder sb = new StringBuilder();
        for(String s : parts){
            if(s.equals("25")){
                sb.append("dd/");
            }
            if(s.equals("12")){
                sb.append("MM/");
            }
            if(s.equals("2013")){
                sb.append("yyyy/");
            }
        }
        return sb.toString().substring(0, sb.toString().length()-1);
    }

EDIT Please check the Mark Melling's answer below https://stackoverflow.com/a/18982842/945808 to have better solution. Mine was just a hack long time ago.

toantran
  • 1,789
  • 17
  • 25
  • 1
    This will only work with Locales that split the date with `/` what is `.` or `-` is used for that locale – draksia Aug 24 '16 at 20:28
  • @draksia Then pls check the later answer from Mark Melling http://stackoverflow.com/a/18982842/945808 – toantran Aug 25 '16 at 02:35
  • Yeah saw that I really need it for specific locales, I made some progress today hopefully it works – draksia Aug 25 '16 at 02:37
  • Great, I hope the author of this question could change the accepted answer to Mark Melling's answer. My answer was just a hack at that time. – toantran Aug 25 '16 at 02:39
  • @VladMatvienko it is! as I said in the edit, pls check the answer from mark melling below – toantran Jan 23 '18 at 00:05
5

There is a static method in the API that you can call like this:

Format dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());

There is more discussion about it here.

Community
  • 1
  • 1
lequebecois
  • 173
  • 2
  • 6
  • That dateFormat object has a private field called pattern which is exactly what I am looking for. But it is private and has no getter to get it. How can I get it without reflection? – Androrider Jul 29 '13 at 20:16
2

You can use this:

private static DateFormat mDateFormat;
private static DateFormat mTimeFormat;

mDateFormat = android.text.format.DateFormat.getDateFormat(this);
mTimeFormat = android.text.format.DateFormat.getTimeFormat(this);

public static String getSystemDateFormat() {
    return ((SimpleDateFormat) mDateFormat).toPattern();
}

public static String getSystemTimeFormat() {
    return ((SimpleDateFormat) mTimeFormat).toPattern();
}

public static String getSystemDateTimeFormat() {
    return getSystemDateFormat() + " " + getSystemTimeFormat();
}
1

based on an answer above:

String pattern = Settings.System.getString(getActivity().getContentResolver(),
Settings.System.DATE_FORMAT);
                String format;
                if (pattern.indexOf("d")<pattern.indexOf("M"))
                    format = "d/M";
                else
                    format = "M/d";
                SimpleDateFormat df = new SimpleDateFormat(format);

and then use the SimpleDateFormat to format your Date objects. It's working for me.

devrocca
  • 2,497
  • 2
  • 19
  • 27
1

SimpleDateFormat

I use SimpleDateFormat without custom pattern to get actual date and time in preferred format from system:

public static String getFormattedDate() {
    Calendar c = Calendar.getInstance();
    SimpleDateFormat df = new SimpleDateFormat(); //called without pattern
    return df.format(c.getTime());
}

returns:

  • 13.01.15 11:45
  • 1/13/15 10:45 AM
  • ...
Community
  • 1
  • 1
Tomas
  • 4,652
  • 6
  • 31
  • 37
0
String shortDateFormat  = Settings.System.getString(context.getContentResolver(), Settings.System.DATE_FORMAT);
laalto
  • 150,114
  • 66
  • 286
  • 303
guest
  • 141
  • 1
  • 3
  • My system is set to return dd/MM/yyyy and this gives me dd-MM-YYYY - so not the correct seperators. The answer above by Mark Melling however does return the correct system pattern. – slott Feb 25 '14 at 21:00
  • got it from docs: [android dev](http://developer.android.com/reference/android/provider/Settings.System.html#DATE_FORMAT). Anyway answer above by Mark Melling - better solution. – guest Apr 23 '14 at 12:37
0

According to the DateFormat documentation:

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

myString = DateFormat.getDateInstance().format(myDate);

And to format it for a different locale:

myString = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE).format(myDate);
Marvin Pinto
  • 30,138
  • 7
  • 37
  • 54
  • 4
    Seems to be that my question has been misunderstood. What I need is to get the current date format string which is used by the system when DateFormat.getLongDateFormat(Context context).format(Date date) is called. I dont need the formatted date string. What I need is the format string itself. Like "mm/dd/yyyy" or whichever the system uses according to the locale set on the device. Thanks. – Androrider Feb 18 '12 at 23:35
  • Not possible in Android. The method getDateInstance() is undefined for the type DateFormat. – slott Feb 25 '14 at 21:03
  • Causes java.lang.IllegalArgumentException: Bad class: class java.util.GregorianCalendar – Greg Ennis Mar 27 '15 at 03:36
  • Does any one know how to get the actual pattern like yyyy-MM-dd? – draksia Aug 24 '16 at 20:22