前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >year 和 weak year 的区别

year 和 weak year 的区别

作者头像
xiaoxi666
发布2018-12-07 17:49:06
1.4K0
发布2018-12-07 17:49:06
举报
文章被收录于专栏:xiaoxi666的专栏xiaoxi666的专栏

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 的代码:

代码语言:javascript
复制
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)是这样的。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-11-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • google 答案:
  • Stackoverflow的一个具体问题及其解释:
  • 查看源码
  • 结论
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档