`````````````````yyyy```` [英] `uuuu` versus `yyyy` in `DateTimeFormatter` formatting pattern codes in Java?
问题描述
DateTimeFormatter
类文档说明了其年度的格式代码:
你2004年的一年04
y年份2004年; 04
...
年份:字母数确定最小字段宽度低于该填充使用。如果字母数为2,则使用缩减的两位数形式。对于打印,这将输出最右边的两位数字。对于解析,这将使用基数值2000解析,导致一年在2000到2099之间的范围内。如果字母数小于四(但不是两个),则符号只能按照SignStyle.NORMAL输出为负数。否则,如果超出了垫宽度,则按照SignStyle.EXCEEDS_PAD的方式输出符号。
没有其他提到时代。
那么这两个代码有什么区别, u
对 y
,年
与年龄
?
应该怎样使用这种模式
uuuu-MM-dd
,而当 yyyy-MM-dd
在Java中使用日期的时候? 看起来像那些知道使用 uuuu 的人编写的示例代码,但为什么?
其他格式化类,如旧版 SimpleDateFormat
只有 yyyy
,所以我很困惑为什么java.time为年代带来这个 uuuu
在javadoc部分中格式化模式和解析 DateTimeFormatter
它列出以下3个相关符号:
符号含义演示文稿示例
------ ------- ------------ -------
G时代文字AD;公元; 2004年A
04年
年2004年; 04
只是为了比较,这些其他符号很容易理解:
D每年的日数189
d每月的日数10
E day-本周文字;星期二; T
日期
日期
和星期几
显然是 给定的范围(年,月,周)。
所以,年龄
意味着年份(在时间范围内),右上角时代
的示例值为 AD
(另一个值当然是 BC
)。
year
是签署的年份,年份
0
是 1 BC
年 -1
是 2 BC
等等。
说明:什么时候凯撒被杀暗杀?
- 公元前44年3月15日(使用模式
MMMM d,y GG
) - 3月15日-43 (使用模式
MMMM d,u
)
当然,只要年份为零或为负,而且由于这是罕见的,大多数人都不关心,即使他们应该。
结论: strong>如果您使用 y
,您还应该使用 G
。由于 G
很少使用,所以正确的年份符号是 u
,而不是 y
,否则非正年将显示不正确。
这被称为防御性编程:
防御性编程是一种防御性设计,
$ b
$ b
请注意, DateTimeFormatter
与 SimpleDateFormat
:
信函日期或时间组件演示示例
------ ---------------- ------------------ --------
G Era指示符文字AD
y年份1996; 96
负面的年份一直是一个问题,他们现在通过添加 u
。
The DateTimeFormatter
class documentation says about its formatting codes for the year:
u year year 2004; 04
y year-of-era year 2004; 04
…
Year: The count of letters determines the minimum field width below which padding is used. If the count of letters is two, then a reduced two digit form is used. For printing, this outputs the rightmost two digits. For parsing, this will parse using the base value of 2000, resulting in a year within the range 2000 to 2099 inclusive. If the count of letters is less than four (but not two), then the sign is only output for negative years as per SignStyle.NORMAL. Otherwise, the sign is output if the pad width is exceeded, as per SignStyle.EXCEEDS_PAD.
No other mention of "era".
So what is the difference between these two codes, u
versus y
, year
versus year-of-era
?
When should I use something like this pattern uuuu-MM-dd
and when yyyy-MM-dd
when working with dates in Java?
Seems that example code written by those in the know use uuuu
, but why?
Other formatting classes such as the legacy SimpleDateFormat
have only yyyy
, so I am confused why java.time brings this uuuu
for "year of era".
In the javadoc section Patterns for Formatting and Parsing for DateTimeFormatter
it lists the following 3 relevant symbols:
Symbol Meaning Presentation Examples
------ ------- ------------ -------
G era text AD; Anno Domini; A
u year year 2004; 04
y year-of-era year 2004; 04
Just for comparison, these other symbols are easy enough to understand:
D day-of-year number 189
d day-of-month number 10
E day-of-week text Tue; Tuesday; T
The day-of-year
, day-of-month
, and day-of-week
are obviously the day within the given scope (year, month, week).
So, year-of-era
means the year within the given scope (era), and right above it era
is shown with an example value of AD
(the other value of course being BC
).
year
is the signed year, where year 0
is 1 BC
, year -1
is 2 BC
, and so forth.
To illustrate: When was Julius Caesar assassinated?
- March 15, 44 BC (using pattern
MMMM d, y GG
) - March 15, -43 (using pattern
MMMM d, u
)
The distinction will of course only matter if year is zero or negative, and since that is rare, most people don't care, even though they should.
Conclusion: If you use y
you should also use G
. Since G
is rarely used, the correct year symbol is u
, not y
, otherwise a non-positive year will show incorrectly.
This is known as defensive programming:
Defensive programming is a form of defensive design intended to ensure the continuing function of a piece of software under unforeseen circumstances.
Note that DateTimeFormatter
is consistent with SimpleDateFormat
:
Letter Date or Time Component Presentation Examples
------ ---------------------- ------------ --------
G Era designator Text AD
y Year Year 1996; 96
Negative years has always been a problem, and they now fixed it by adding u
.
这篇关于`````````````````yyyy````的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!