Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format JodaTime DateTime with preferred DateFormat

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?

like image 409
RominaV Avatar asked Sep 02 '16 20:09

RominaV


People also ask

What is T and Z in datetime?

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).

What is Joda-Time format?

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.

Is Joda Datetimeformatter thread safe?

DateTimeFormat is thread-safe and immutable, and the formatters it returns are as well.

What is Joda-Time in Java?

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.


1 Answers

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.

like image 200
Markus Fischer Avatar answered Oct 14 '22 01:10

Markus Fischer