JtecX JtecX - 2 months ago 28
Android Question

Android - SimpleDateFormat IllegalArgumentException: Unknown pattern character 'u'

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: JDK 8

u
is used in the documentation of
SimpleDateFormat
:


Day number of week (1 = Monday, ..., 7 = Sunday)

Answer

The documentation of SimpleDateFormat is misleading (as of 09/16/2016, for future readers).

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 + "'");
    }
}

To get the current day of the week, you could do something like this:

Calendar calendar = Calendar.getInstance();

// if you do not wish to use the current time, but a specific one
// calendar.setTimeInMillis(yourTimeInMillis);

switch (calendar.get(Calendar.DAY_OF_WEEK)) {
    case Calendar.MONDAY:
        // monday
        break;
    case Calendar.TUESDAY:
        // tuesday
        break;
    // ...
}
Comments