java.text.SimpleDateFormat 毕竟是 java.text.DateFormat 的实现。 DateFormat 的这个实现可以理解以下模式字母
G Era designator
y Year
M Month in year
w Week in year
W Week in month
D Day in year
d Day in month
F Day of week in month
E Day in week
a Am/pm marker
H Hour in day (0-23)
k Hour in day (1-24)
K Hour in am/pm (0-11)
h Hour in am/pm (1-12)
m Minute in hour
s Second in minute
S Millisecond
z Time zone
Z Time zone
java.text.DateFormat还有其他实现可以理解其他模式字母组吗?如果有的话请告诉我。
最佳答案
java.text.SimpleDateFormat
是唯一一个扩展了 java.text.DateFormat
的公开公开的 JDK 类;但是,不能保证 DateFormat
的工厂方法(例如 getDateInstance()
)一定会返回 java.text.SimpleDateFormat
。相反,它们可以返回 JDK 实现提供的其他类的实例。 (即使在这种情况下,我也不确定其他类(class)是否会“理解其他一些模式字母组”,正如您所建议的那样;相反,我猜测如果语言环境的日期解析和格式化需求比简单文本模式所能提供的更复杂,而不是语言环境需要不同模式。但谁知道呢?)
如the Javadoc for java.text.DateFormat
说:
If you want even more control over the format or parsing, (or want to give your users more control), you can try casting the
DateFormat
you get from the factory methods to aSimpleDateFormat
. This will work for the majority of countries; just remember to put it in atry
block in case you encounter an unusual one.
如果需要,您可以使用自己的日期格式子类扩展 DateFormat
。它的构造函数是 protected
,而不是包私有(private)的,因此您可以自由地子类化它。
关于java - 除了 java.text.SimpleDateFormat 之外,还有 java.text.DateFormat 的其他实现吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14722362/