Android : Error SimpleDateFormat Unknown pattern character 'u'

我们两清 提交于 2019-12-30 11:16:15

问题


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;
}

回答1:


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



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/20177268/android-error-simpledateformat-unknown-pattern-character-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!