Y 返回 2012 而 y 在 SimpleDateFormat 中返回 2011 [英] Y returns 2012 while y returns 2011 in SimpleDateFormat

查看:18
本文介绍了Y 返回 2012 而 y 在 SimpleDateFormat 中返回 2011的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么在 SimpleDateFormat 中Y"返回 2012 而y"返回 2011:

I wonder why 'Y' returns 2012 while 'y' returns 2011 in SimpleDateFormat:

System.out.println(new SimpleDateFormat("Y").format(new Date())); // prints 2012
System.out.println(new SimpleDateFormat("y").format(new Date())); // prints 2011

谁能解释一下为什么?

推荐答案

周年和年.来自 javadoc

week year and year. From javadoc

一周年与 WEEK_OF_YEAR 周期同步.之间的所有周第一周和最后一周(含)具有相同的周年值.因此,一周的第一天和最后一天可能会有所不同日历年值.

A week year is in sync with a WEEK_OF_YEAR cycle. All weeks between the first and last weeks (inclusive) have the same week year value. Therefore, the first and last days of a week year may have different calendar year values.

例如,1998 年 1 月 1 日是星期四.如果 getFirstDayOfWeek() 是MONDAY 和 getMinimalDaysInFirstWeek() 为 4(ISO 8601 标准兼容设置),然后 1998 年的第 1 周从 1997 年 12 月 29 日开始,并于 1998 年 1 月 4 日结束.最后三个星期的年份为 19981997 日历年的天数.但是,如果 getFirstDayOfWeek() 是SUNDAY,然后是 1998 年的第 1 周,从 1998 年 1 月 4 日开始,到1998 年 1 月 10 日;1998 年的前三天是一周的一部分1997 年的第 53 位,他们的周年份是 1997 年.

For example, January 1, 1998 is a Thursday. If getFirstDayOfWeek() is MONDAY and getMinimalDaysInFirstWeek() is 4 (ISO 8601 standard compatible setting), then week 1 of 1998 starts on December 29, 1997, and ends on January 4, 1998. The week year is 1998 for the last three days of calendar year 1997. If, however, getFirstDayOfWeek() is SUNDAY, then week 1 of 1998 starts on January 4, 1998, and ends on January 10, 1998; the first three days of 1998 then are part of week 53 of 1997 and their week year is 1997.

这篇关于Y 返回 2012 而 y 在 SimpleDateFormat 中返回 2011的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆