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)
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)
To get the date format pattern you can do:
Format dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
String pattern = ((SimpleDateFormat) dateFormat).toLocalizedPattern();
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.
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.
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();
}
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.
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:
String shortDateFormat = Settings.System.getString(context.getContentResolver(), Settings.System.DATE_FORMAT);
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);