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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 06:47:40  来源:igfitidea点击:

Java DateFormat

javadate-format

提问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

mand Dhave their own meaning in SimpleDateFormat pattern:
http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

mD在 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 Yin 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 :

您不仅在更改大小写,还更改了格式的含义:

  • Ydoesn't exist.
  • Mstands for Month in year
  • mstands for Minute in hour
  • Dstands for Day in year
  • dstands for Day in month
  • Y不存在。
  • M代表月份
  • m代表分钟
  • D代表一年中的一天
  • d代表月中的第几天

DD-MMM-YYYYand dd-mm-YYYYformats have no meaning.

DD-MMM-YYYYdd-mm-YYYY格式没有意义。

More info on SimpleDateFormat

有关 SimpleDateFormat 的更多信息

回答by user3215445

use (lowercase) y for year.

使用(小写)y 表示年份。

M = month
m = minute
d = 1-30 (or 31)
D = 1-365