year 和 weak year 的区别

java 中使用 SimpleDateFormat 时,会遇到 year 和 week year 这两个概念,特此记录。

google 答案:

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 yearvalues.

Stackoverflow的一个具体问题及其解释:

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.

查看源码

​java8(jdk1.8.0_171) 中,SimpleDateFormat.java 文件中的 subFormat 函数处理两种 year 的代码:

case PATTERN_WEEK_YEAR: // 'Y'
case PATTERN_YEAR:      // 'y'
    if (calendar instanceof GregorianCalendar) {
        if (count != 2) {
            zeroPaddingNumber(value, count, maxIntCount, buffer);
        } else {
            zeroPaddingNumber(value, 2, 2, buffer);
        } // clip 1996 to 96
    } else {
        if (current == null) {
            zeroPaddingNumber(value, style == Calendar.LONG ? 1 : count,
                              maxIntCount, buffer);
        }
    }
    break;

结论

以上源码说明,year 和 week year 的处理方式一致。至少在java8(jdk1.8.0_171)是这样的。

posted @   xiaoxi666  阅读(2628)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
TOP
点击右上角即可分享
微信分享提示