java date 格式化 周,Java Time使用DateTimeFormatter解析基于星期的周模式

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

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.

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));

I get this:

Parsing 2016-01-03, expected 2015-53 but got 2016-02

2016-01-03 as ISO week date: 2015-W53-7

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.

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();

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值