JtecX JtecX - 2 months ago 11
Android Question

Android - Error SimpleDateFormat Unknown pattern character 'u'

Please help:
Android Studio Logcat:

FATAL EXCEPTION: main
java.lang.IllegalArgumentException: Unknown pattern character 'u'


Here is my Code:

long ticket = 1473808310826L;
SimpleDateFormat sdf = new SimpleDateFormat("u");
String test = sdf.format(ticket);


using: jdk1.8

u is used in the Android Documentation
https://developer.android.com/reference/java/text/SimpleDateFormat.html

Trying to use:
Day number of week (1 = Monday, ..., 7 = Sunday)

Answer

The documentation of SimpleDateFormat is misleading.

There is no u pattern character in Android.

From the source code of SimpleDateFormat:

static final String PATTERN_CHARS = "GyMdkHmsSEDFwWahKzZLc";

// ...

private void validatePatternCharacter(char format) {
    int index = PATTERN_CHARS.indexOf(format);
    if (index == -1) {
        throw new IllegalArgumentException("Unknown pattern character '" + format + "'");
    }
}