Java 日期格式
我知道 Java 日期时间不是一个好的前进方式,但我只是好奇发生了什么:为什么
下面这一行:
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.US)
不会产生任何错误,以下行会产生任何错误:
DateFormat df = new SimpleDateFormat("DD-MMM-YYYY", Locale.US)
DateFormat df = new SimpleDateFormat("dd -mm-YYYY", Locale.US)
抛出以下异常:
Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'Y'
at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:769)
at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:576)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:501)
at testing.MySchedule.main(MySchedule.java:18)
我的意思是我只是改变大小写,对吗?但是 DateFormat 真的那么愚蠢还是我做错了什么?或者它与我正在使用的区域设置有关?
干杯
I know Java Date Time is not a good way to go forward but I was just curious as to what's happening:
Why does the following line:
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.US)
not produce any errors and the following lines do:
DateFormat df = new SimpleDateFormat("DD-MMM-YYYY", Locale.US)
DateFormat df = new SimpleDateFormat("dd-mm-YYYY", Locale.US)
The following exception gets thrown:
Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'Y'
at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:769)
at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:576)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:501)
at testing.MySchedule.main(MySchedule.java:18)
I mean I'm just changing the case right? but is DateFormat really that dumb or am I doing something wrong? or does it have something to do with the Locale I'm using?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
m
和D
在 SimpleDateFormat 模式中有自己的含义:http://download.oracle.com/javase/6 /docs/api/java/text/SimpleDateFormat.html
但是您在该表中找不到
Y
。m
andD
have their own meaning in SimpleDateFormat pattern:http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
But you won't find
Y
in that table.您不仅更改了大小写,还更改了格式的含义:
Y
不存在。M
代表一年中的月份m
代表小时中的分钟D
代表一年中的某天d
代表一月中的某天DD-MMM-YYYY
和dd-mm- YYYY
格式没有任何意义。有关 SimpleDateFormat 的详细信息
You aren't changing only the case, you are changing the meaning of the format :
Y
doesn't exist.M
stands for Month in yearm
stands for Minute in hourD
stands for Day in yeard
stands for Day in monthDD-MMM-YYYY
anddd-mm-YYYY
formats have no meaning.More info on SimpleDateFormat
它不是“愚蠢的”,它只是一个无效的模式。看看API:
SimpleDateFormat - J2SE 6 也已使用 SimpleDateFormat在 J2SE 7 中更新并允许现在使用 Y SimpleDateFormat - J2SE 7
It's not "dumb", it's just an invalid pattern. Have a look at the API:
SimpleDateFormat - J2SE 6 also SimpleDateFormat usage has been updated in J2SE 7 and allows using Y now SimpleDateFormat - J2SE 7
使用(小写)y 表示年份。
use (lowercase) y for year.