简体   繁体   中英

Android : Error SimpleDateFormat Unknown pattern character 'u'

I use java 1.7.25 but found this error. what should I do?

FATAL EXCEPTION: main
java.lang.IllegalArgumentException: Unknown pattern character 'u'
        at java.text.SimpleDateFormat.validateFormat(SimpleDateFormat.java:264)
        at java.text.SimpleDateFormat.validatePattern(SimpleDateFormat.java:319)
        at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:365)
        at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:249)

Here is my code

    public static int getDayNumberOfWeek(int day, String monthString, int yyyy) {
//http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
    int dayNumberOfWeek = 1;
    final String inputFormat = "MMM/dd/yyyy";
    final String outputFormat = "u";
    String dayString2Digit = DateTimeHelper.getTwoDigit(day);
    String inputTimeStamp = monthString + "/" + dayString2Digit + "/" + String.valueOf(yyyy);
    try {
        dayNumberOfWeek =Integer.valueOf(TimeStampConverter(inputFormat, inputTimeStamp,
                                                            outputFormat));
    }
    catch (ParseException e) {
        e.printStackTrace();
    }
    return dayNumberOfWeek;
}

I use java 1.7.25

No, you don't - not if you're running on Android. You need to look at the Android documentation, not the Java 7 docs.

If you look at the Android SimpleDateFormat documentation you'll see that u isn't listed there. I don't believe there's a format pattern character for "day of week as a number" in Android.

Were you really looking for that though? If you just want the day of the week as a number (without anything else) you can always use

String text = String.valueOf(calendar.get(Calendar.DAY_OF_WEEK));

If you're using android, then you're not using Java 1.7.25. See the android documentation : there's no support for u in SimpleDateFormat.

I'm guessing your problem is going to be in your TimeStampConverter class where you're passing in that "u" as the outputFormat . "u" is not a valid format character in SimpleDateFormat and you must be constructing a format string that contains it.

If you need to use the "u" as a literal, you'll need to enclose it in single quotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM