I'm using Joda Time and need to display a date in the user's preferred format (note that before Android M, the format could be changed).
A Joda DateTime can be formatted using DateTimeFormatter, which is created from a String with the desired Date format:
public String getFormattedDate(String datePattern) {
if (mDate != null) {
// get your local timezone
DateTimeZone localTZ = DateTimeZone.getDefault();
DateTime dt = mDate.toDateTime(localTZ);
DateTimeFormatter fmt = DateTimeFormat.forPattern(datePattern);
String formattedDate = dt.toString(fmt);
return formattedDate;
}
return "";
}
but to get the user's preferred format, you have to use Java DateFormat:
public static DateFormat getPreferredDateFormat(Context context) {
final String format = Settings.System.getString(context.getContentResolver(), Settings.System.DATE_FORMAT);
DateFormat dateFormat;
if (android.text.TextUtils.isEmpty(format)) {
dateFormat = android.text.format.DateFormat.getMediumDateFormat(context.getApplicationContext());
} else {
dateFormat = android.text.format.DateFormat.getDateFormat(context.getApplicationContext()); // Gets system date format
}
if (dateFormat == null)
dateFormat = new SimpleDateFormat(format);
return dateFormat;
}
And Java DateFormat doesn't have a method which can give me a String with a date format in it.
So is there a way to format a Joda DateTime with Java DateFormat? And maybe also specify that I only want to show day and month (would be dd/MM or MM/dd) ? Or to make DateTimeFormatter take the user's preferred format?