Java Time使用DateTimeFormatter解析基于星期的周模式 [英] Java Time's week-of-week-based-year pattern parsing with DateTimeFormatter

查看:1558
本文介绍了Java Time使用DateTimeFormatter解析基于星期的周模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要输出当前日期格式为基于周的年度 - 基于周工作周的年度,即使用 ISO周日期,其中一周始终在星期一开始,一年中的第一个星期是第一个星期一月至少有四天(因此是一月的第一个星期四)。

I need to output the current date in the format week-based-year-week-of-week-based-year, i.e. using the ISO week date where the week always starts on a Monday and the first week of the year is the first one that has at least four days in January (so the week with the first Thursday in January).

自2015年12月31日以来星期四,即星期五至星期日,即2016年1月1日至3日,均属于2015年第53周(长年),2016年第一周均于1月4日星期一开始。

Since 31 December 2015 was a Thursday, the Friday through Sunday, i.e. 1st through 3rd of January 2016, all belong in the 53rd week of 2015 (a "long year"), and the first week of 2016 starts on Monday, 4 January.

来自 DateTimeFormatter规范,我原本预计我可以使用模式 YYYY-ww 来做到这一点( Y 基于周的年度 w 周工作周基年)。

From the DateTimeFormatter spec, I would have expected that I can just use the pattern YYYY-ww to do that (Y is week-based-year and w is week-of-week-based-year).

然而,当我尝试类似下面的简化测试用例时:

However, when I try something like the following simplified test case:

String dateString = "2016-01-03";
LocalDate date = LocalDate.parse(dateString, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
String expectedOutput = "2015-53";
String actualOutput = date.format(DateTimeFormatter.ofPattern("YYYY-ww"));
System.out.println("Parsing " + dateString + ", expected "
        + expectedOutput + " but got " + actualOutput);
System.out.println(dateString + " as ISO week date: "
        + date.format(DateTimeFormatter.ISO_WEEK_DATE));

我明白了:


解析2016-01-03,预计2015-53但得到2016-02

2016-01-03作为ISO周日期:2015-W53-7

所以使用内置的 ISO_WEEK_DATE 格式化程序可以达到我的预期,但使用模式 YYYY-ww 似乎给了我日历年份和周。

So using the built-in ISO_WEEK_DATE formatter does what I expected, but using the pattern YYYY-ww seems to give me the calendar year and week.

我是否误解了ISO周日期,或者我的代码中是否有错误,或者......我敢说...这是 java.time 库中的一个错误(就像这个问题)?

Have I misunderstood something about ISO week dates, or is there an error in my code, or... dare I say it... is this a bug in the java.time library (as was the case for this question)?

我知道我可以使用下面的自定义格式化器解决这个问题,但在我的情况下我确实需要使用模式工作。

I know I could work around this using a custom formatter like below, but in my case I really need this to work using a pattern.

new DateTimeFormatterBuilder()
    .parseCaseInsensitive()
    .appendValue(IsoFields.WEEK_BASED_YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
    .appendLiteral("-")
    .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 2)
    .toFormatter();


推荐答案

DateTimeFormatterBuilder 指定Y附加本地化的基于周的年份。因此,基于周的年和周字段的含义将取决于格式化程序中设置的区域设置。

The documentation of DateTimeFormatterBuilder specifies that "Y" appends the localized week-based year. Thus, the meaning of the week-based year and week fields will depend on the locale set in the formatter.

这篇关于Java Time使用DateTimeFormatter解析基于星期的周模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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