DateTimeFormatter为边缘情况提供了错误的格式 [英] DateTimeFormatter giving wrong format for edge cases

查看:90
本文介绍了DateTimeFormatter为边缘情况提供了错误的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DateTimeFormatter根据以下代码片段未提供2018年12月30日和31日的正确格式.

DateTimeFormatter is not giving correct format for Dec 30 and 31 2018 as per following snippet.

final String DATE_FORMAT = "YYYYMM";
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(DATE_FORMAT);
LocalDateTime startDate = LocalDateTime.of(2018,12,29,5,0,0);
System.out.println(startDate.format(dateFormat));
//prints 201812
LocalDateTime startDate = LocalDateTime.of(2018,12,30,5,0,0);
System.out.println(startDate.format(dateFormat));
//prints 201912 <------ should be 201812
LocalDateTime startDate = LocalDateTime.of(2018,12,31,5,0,0);
System.out.println(startDate.format(dateFormat));
//prints 201912 <------ should be 201812

这是预期的行为还是DateTimeFormatter出现错误?

Is this the expected behavior or is there a bug with DateTimeFormatter?

推荐答案

YYYY是星期年,yyyy是年

YYYY is week year, yyyy is year

因此,更改final String DATE_FORMAT = "YYYYMM"; ro final String DATE_FORMAT = "yyyyMM";应该会给您正确的结果.有关这些模式的更多信息,请参见 javadoc of DateTimeFormatter .

So Change final String DATE_FORMAT = "YYYYMM"; ro final String DATE_FORMAT = "yyyyMM"; should give you the correct result. For more informations about the patterns see the javadoc of DateTimeFormatter.

2019年的第一周从2018年12月30日开始.有关更多信息,请参见链接关于凌年的信息

The first week of 2019 starts at Dec 30 of 2018. See this link for more informations about the wee years

这篇关于DateTimeFormatter为边缘情况提供了错误的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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