使用SimpleDateFormat时,未知的模式字符'x' [英] Unknown pattern character 'x', when using SimpleDateFormat

查看:717
本文介绍了使用SimpleDateFormat时,未知的模式字符'x'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SimpleDateFormat Date 格式化为 String c>,并且模式im正在使用的就是这个

Im trying to format a Date to String using SimpleDateFormat, and the pattern im using is this one


yyyy-MM-dd'T'HH:mm:ss.SSSxxx

"yyyy-MM-dd'T'HH:mm:ss.SSSxxx"

但是到达此行时

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSxxx");

我得到以下异常:


java.lang.IllegalArgumentException:
处的未知模式字符'x'java.text.SimpleDateFormat.validatePatternCharacter(SimpleDateFormat.java:323)

java.text .SimpleDateFormat.validatePattern(SimpleDateFormat.java:312)
在java.text.SimpleDateFormat。(SimpleDateFormat.java:365)在
java.text.SimpleDateFormat。(SimpleDateFormat.java:258)

java.lang.IllegalArgumentException: Unknown pattern character 'x' at java.text.SimpleDateFormat.validatePatternCharacter(SimpleDateFormat.java:323) at java.text.SimpleDateFormat.validatePattern(SimpleDateFormat.java:312) at java.text.SimpleDateFormat.(SimpleDateFormat.java:365) at java.text.SimpleDateFormat.(SimpleDateFormat.java:258)

我试图实现的格式是
2017-06-16T12:19:59.001 + 02:00

根据文档这种模式应该可行
出什么问题了?

according to the documentation this pattern should work Whats wrong?

编辑
为了澄清,我尝试了带有XXX和XXX

EDIT To clarify, i tried with xxx and XXX

如果是XXX,我得到java.lang.IllegalArgumentException:未知模式字符'X'

in case of XXX i get java.lang.IllegalArgumentException: Unknown pattern character 'X'

推荐答案

请参阅下面的更新。



不幸的是,关于大写的 X ,文档[在提出问题和回答时]错误。由于文档似乎具有 x,web.archive.org / web / 20160331172034 / http://developer.android.com/reference/java/text/SimpleDateFormat.html rel = noreferrer>发生重大变化 X ,我继续在这里说明,现在说 X (大写)受支持(自API级别1开始),但根本没有提及 x (小写)。 以前用于也不要提及 X

See Update below.

Unfortunately, regarding uppercase X, the documentation [was at the time of the question and this answer] wrong. Since the documentation seems to have changed significantly regarding x and X, I'll go ahead and state here that right now, it says X (uppercase) is supported (since API level 1), but x (lowercase) is not mentioned at all. The docs used to not mention X either.

检查 Android源代码(请参见validateFormat())显示尽管该文档声称自API级别1起已支持 X ,但该版本中仅识别字母 GyMdkHmsSEDFwWahKzZLc ,这说明了为什么您得到 IllegalArgumentException:未知模式字符'X'

A check of the Android source code (see validateFormat()) shows that only the letters GyMdkHmsSEDFwWahKzZLc are recognized in that version, despite the docs' claim that X has been supported since API level 1. This explains why you're getting the IllegalArgumentException: Unknown pattern character 'X'.

请参见此错误报告以了解历史详情。

See this bug report for historical details.

同时,您将必须找到一种解决方法,具体方法取决于您需要解析哪种输入。例如。 OP的答案

Meanwhile, you'll have to find a workaround, which will vary depending on what kind of input you need to parse. E.g. the OP's answer.

文档中的现已修复添加受支持(API级别)列,该列指示仅从API级别24开始才支持 X 。大概是OP的 IllegalArgumentException 是由于之前在文档中未提及支持的API级别,因此在24之前的设备上测试了该应用。

The documentation has now been fixed to add a "Supported (API Levels)" column, which indicates that X is only supported starting from API level 24. Presumably the OP's IllegalArgumentException was due to testing the app on a pre-24 device, since the docs didn't say anything about supported API levels before.

这篇关于使用SimpleDateFormat时,未知的模式字符'x'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆