Java日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3914295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java DateFormat
提问by Achilles
I know Java Date Time is not a good way to go forward but I was just curious as to what's happening:
我知道 Java Date Time 不是一个好的前进方式,但我只是对正在发生的事情感到好奇:
Why does the following line:
为什么会出现以下行:
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.US)
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-MMM-YYYY", Locale.US)
DateFormat df = new SimpleDateFormat("dd-mm-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?
我的意思是我只是在改变情况,对吗?但是 DateFormat 真的那么愚蠢还是我做错了什么?还是与我使用的语言环境有关?
Cheers
干杯
采纳答案by Nikita Rybak
m
and D
have their own meaning in SimpleDateFormat pattern:
http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
m
并D
在 SimpleDateFormat 模式中有自己的含义:http:
//download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
m Minute in hour
D Day in year
But you won't find Y
in that table.
但你不会Y
在那个表中找到。
回答by cherouvim
It's not "dumb", it's just an invalid pattern. Have a look at the API: SimpleDateFormat - J2SE 6also SimpleDateFormat usage has been updated in J2SE 7 and allows using Y now SimpleDateFormat - J2SE 7
这不是“愚蠢”,它只是一个无效的模式。看看 API: SimpleDateFormat - J2SE 6SimpleDateFormat 用法也已在 J2SE 7 中更新并允许现在使用 Y SimpleDateFormat - J2SE 7
回答by Julien Hoarau
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 month
Y
不存在。M
代表月份m
代表分钟D
代表一年中的一天d
代表月中的第几天
DD-MMM-YYYY
and dd-mm-YYYY
formats have no meaning.
DD-MMM-YYYY
和dd-mm-YYYY
格式没有意义。
回答by user3215445
use (lowercase) y for year.
使用(小写)y 表示年份。
M = month
m = minute
d = 1-30 (or 31)
D = 1-365