在SimpleDateFormat模式字符串中使用字母字符 [英] Using Alphabetic Characters in SimpleDateFormat Pattern String

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

问题描述

可以在SimpleDateFormat Pattern String中包含一个字母字符吗?

Is it possible to include an alphabetic character in a SimpleDateFormat Pattern String?

我正在尝试创建一个格式字符串,其中包含字母T时间例如:

I am trying to create a format string where the letter 'T' is included before the time for example:

2003-11-15 T 09:30:47-05:00。

2003-11-15T09:30:47-05:00.

我正在使用 yyyy-MM-ddhh:mm:ssZ 作为模式字符串。

I am currently using yyyy-MM-ddhh:mm:ssZ as the pattern string.

如果我将其更改为 yyyy-MM-ddThh:mm:ssZ 它会将'A'引发到'Z'和'a'到'z '保留。

If I change it to yyyy-MM-ddThh:mm:ssZ it will throw an exception as 'A' to 'Z' and 'a' to 'z' are reserved.

有没有任何解决方案不涉及使用两个DateFormats?

Is there any solution that does not involve using two DateFormats?

推荐答案

使用单引号将 T 环绕起来应该可以工作:

Surrounding the T with single quotes should work:

yyyy-MM-dd'T'hh:mm:ssZ

文档(强调我的):


日期和时间格式由日期和时间模式字符串指定。在日期和时间模式字符串中,从'A''Z''a' to 'z'被解释为表示日期或时间字符串组件的模式字母。 可以使用单引号(')引用文本,以避免解释。 代表单引号。所有其他字符不被解释;在格式化过程中,它们将被简单地复制到输出字符串中,或​​者在解析时与输入字符串匹配。

Date and time formats are specified by date and time pattern strings. Within date and time pattern strings, unquoted letters from 'A' to 'Z' and from 'a' to 'z' are interpreted as pattern letters representing the components of a date or time string. Text can be quoted using single quotes (') to avoid interpretation. "''" represents a single quote. All other characters are not interpreted; they're simply copied into the output string during formatting or matched against the input string during parsing.

您的具体用例甚至包括作为示例:

Your specific use case is even included as an example:

Date and Time Pattern            Result
-------------------------------------------------------------
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"     2001-07-04T12:08:56.235-0700  

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

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