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?
What is T between date and time? The T is just a literal to separate the date from the time, and the Z means “zero hour offset” also known as “Zulu time” (UTC). If your strings always have a “Z” you can use: SimpleDateFormat format = new SimpleDateFormat( “yyyy-MM-dd'T'HH:mm:ss).
Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters. Mid level - pattern-based, like SimpleDateFormat. Low level - builder.
DateTimeFormat is thread-safe and immutable, and the formatters it returns are as well.
Joda-Time is an API created by joda.org which offers better classes and having efficient methods to handle date and time than classes from java. util package like Calendar, Gregorian Calendar, Date, etc. This API is included in Java 8.0 with the java.
DateFormat is an abstract class, and as such doesn't have an access method to the format pattern (as each concrete implementation will handle its own pattern). However, what android.text.format.DateFormat.getDateFormat() returns is actually a SimpleDateFormat, which provides the pattern. Therefore you can do something like:
SimpleDateFormat format=(SimpleDateFormat)DateFormat.getDateFormat(context.getApplicationContext());
String pattern=format.toPattern();
or
String pattern=format.toLocalizedPattern();
This works for the time being, but please note that it's not 100% future proof, as the actual class returned by getDateFormat() may change in the future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With