I use Joda Time and should display the date in my preferred user format ( note that before Android M the format could be changed ).
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) {
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 your preferred user 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());
}
if (dateFormat == null)
dateFormat = new SimpleDateFormat(format);
return dateFormat;
}
And Java DateFormat does not have a method that can give me a String with the date format in it.
So, is there a way to format a Joda DateTime date with a Java DateFormat? And perhaps also indicate that I want to show only the day and month (will there be dd / MM or MM / dd)? Or to make DateTimeFormatter preferable for the user?