java format 02x,使用SimpleDateFormat时,未知的模式字符'x'

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"

but when reach this line

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

i get the following exception:

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)

the format im trying to achieve is

"2017-06-16T12:19:59.001+02:00"

according to the documentation this pattern should work

Whats wrong?

EDIT

To clarify, i tried with xxx and XXX

in case of XXX i get java.lang.IllegalArgumentException: Unknown pattern character '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.

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.

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.

Update: X is available only from Nougat+.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值