英文:
BirtDateTime.addDay returns wrong year around new years eve
问题
我有一个包含两个参数的Birt报告,分别是startDate和endDate(都是文本字段/字符串)。它们的预填值分别是昨天和昨天 + ' 23:59:59',所以格式看起来像这样:2019-12-20
和 2019-12-20 23:59:59
。
这个函数是用来生成日期的:Formatter.format(BirtDateTime.addDay(BirtDateTime.today(), -1), "YYYY-MM-dd")
和 Formatter.format(BirtDateTime.addDay(BirtDateTime.today(), -1), "YYYY-MM-dd 23:59:59")
分别用于生成这两个日期。
这个方法一直正常工作,直到年底。生成的日期如下所示:
BirtDateTime.today() | 生成的日期 |
---|---|
2019-12-29 | 2019-12-28 |
2019-12-30 | 2020-12-29 |
2019-12-31 | 2020-12-30 |
2020-01-01 | 2020-12-31 |
2019-01-02 | 2020-01-01 |
如您所见,从2019-12-30到2020-01-01,addDay函数生成的日期年份出现错误。我猜测这可能与日历周有关(因为新的一周从2019-12-30开始,但在1月2日一切都恢复正常...)。
如果我将BirtDateTime.today()替换为固定字符串"2019-12-31"
或类似的内容,我可以重现这个问题。如果我使用另一年,比如2018年,只有2019-01-01会返回错误的日期(而不是2018-12-31)。
是否有什么我没有注意到的问题,或者有人遇到过这种行为?如果有关的话,我使用的是BIRT 2019-03版本。
英文:
I have a birt report with two parameters, startDate and endDate (which are both text-fields / strings). They are prefilled with yesterday and yesterday + ' 23:59:59', so the format looks like 2019-12-20
and 2019-12-20 23:59:59
.
The function for this is Formatter.format(BirtDateTime.addDay(BirtDateTime.today(), -1), "YYYY-MM-dd")
and Formatter.format(BirtDateTime.addDay(BirtDateTime.today(), -1), "YYYY-MM-dd 23:59:59")
respectively.
This works fine until the end of this year. The generated dates look like this:
| BirtDateTime.today() | Generated date |
| -------------------- | -------------- |
| 2019-12-29 | 2019-12-28 |
| 2019-12-30 | 2020-12-29 |
| 2019-12-31 | 2020-12-30 |
| 2020-01-01 | 2020-12-31 |
| 2019-01-02 | 2020-01-01 |
As you can see, from 2019-12-30 until 2020-01-01 the generated dates from the addDay function have the wrong year. My first guess is that it has to do something with the calendar weeks (because a new week starts on 2019-12-30, but on the 2nd of January everything is fine again...
I can reproduce this if I replace BirtDateTime.today() with a fixed string "2019-12-31"
or similar. If I use another year, let's say 2018, only the 2019-01-01 will return a wrong date (2019-31-12 instead of 2018-12-31).
Is there something I haven't seen or came anybody across this behaviour? If it matters, I'm using birt 2019-03.
答案1
得分: 1
我们遇到了完全相同的问题。
由于 Number(BirtDateTime.diffDay(BirtDateTime.today(), BirtDateTime.addDay(BirtDateTime.today(), -10))) 正确(-10),问题出在其他地方。
使用 'yyyy' 而不是 'YYYY' 修复了问题。请参见yyyy vs YYYY。
英文:
We had exactly the same problem.
As Number(BirtDateTime.diffDay(BirtDateTime.today(), BirtDateTime.addDay(BirtDateTime.today(), -10))) was correct (-10) the problem was elsewhere.
Using 'yyyy' instead of 'YYYY' fixed the problem.
See yyyy vs YYYY.
答案2
得分: 1
YYYY
指定使用周年份。应该改用 yyyy
,只是使用实际日期的年份。
正如您可以在 BirtDateTime.java
源代码中看到的那样,在底层使用了 SimpleDateFormat
进行格式化。
SimpleDateFormat
的 Javadoc 中说
>Letter | 日期或时间组件
>
>y | 年份
>
>Y | 周年份
英文:
YYYY
specifies using the week year. Use should instead using yyyy
to simply use the year on the actual day.
As you can see in the BirtDateTime.java
source that under the hood the SimpleDateFormat
is used for the formatting.
The Javadoc of SimpleDateFormat
says
>Letter | Date or Time Component
>
>y | Year
>
>Y | Week year
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论