有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

2016年1月1日的爪哇Joda时间返回yyyyww,截至201653年

DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyyww");
dateTimeFormatter.print(new DateTime(2016, 1, 1, 1, 1).withZone(DateTimeZone.UTC))

返回201653

为什么是2016年的第53周而不是2015年


共 (2) 个答案

  1. # 1 楼答案

    因为1月1日是星期五。在这种情况下,这一周算作2015年的最后一周,2016年的第一周将从第一个星期一(1月4日)开始
    这是根据ISO 8601标准实施的:

    There are several mutually equivalent and compatible descriptions of week 01:
    - the week with the year's first Thursday in it (the formal ISO definition),
    - the week with 4 January in it,
    - the first week with the majority (four or more) of its days in the starting year, and
    - the week starting with the Monday in the period 29 December – 4 January.

  2. # 2 楼答案

    你要找的是以周为基础的年份(符号x),而不是纪年(符号y)。另请参见pattern syntax used by Joda-Time,它与SimpleDateFormat或Java-8不同。所以解决方案应该是这样的:

    DateTimeFormatter f = DateTimeFormat.forPattern("xxxxww");
    String s = f.print(new DateTime(2016, 1, 1, 1, 1).withZone(DateTimeZone.UTC));
    System.out.println(s); // 201553