简体   繁体   English

添加月份时出错日期

[英]Getting wrong date when I add months

I am writing my stubs in StubbyDB . 我在StubbyDB中编写我的存根。 And asserting the data in functional tests. 并在功能测试中断言数据。 This is something I am doing in my functional tests to calculate date for assertion (using joda datetime library) 这是我在功能测试中所做的事情,用于计算断言的日期(使用joda datetime库)

DateTime now = DateTime.now();
DateTime future = now.plusMonths(6);

And this is something I am doing in my stubs; 这是我在我的存根中做的事情;

{{TODAY+6m}}

But I am getting the difference of few days. 但是我得到了几天的差异。 Is this the bug or am I doing something wrong? 这是错误还是我做错了什么?

Edit 编辑

Consider today is "30 Sept 2016", and I add 5 months to it then 今天考虑的是“2016年9月30日”,然后我又增加了5个月

now.plusMonths(5) => 2017-02-28
{{TODAY+5m}} => 2017-03-02

Reason 原因

As per joda-time documentation, 根据joda-time文档,

2007-03-31 plus one month cannot result in 2007-04-31, so the day of month is adjusted to 2007-04-30. 2007-03-31加上一个月不能导致2007-04-31,所以将月份的日期调整为2007-04-30。

However StubbyDB use javascript based date calculation which adjust date 2007-04-31 to 2007-05-01. 但是,StubbyDB使用基于javascript的日期计算,将日期调整为2007-04-31至2007-05-01。

So this is not the bug but this is how these APIs work. 所以这不是错误,但这就是这些API的工作原理。

Solution

Found in sample application 样本申请中找到

use {{JODA_TODAY+6m}} instead of {{TODAY+6m}} 使用{{JODA_TODAY + 6m}}代替{{TODAY + 6m}}

if you start with 30/09/2016 and add five months you get 30/02/2017. 如果你从2016年9月30日开始,再增加5个月就得到30/02/2017。

But February only has 28 days. 但二月只有28天。

It looks like Jodatime has "rounded down" to give you the maximum valid date for the month (ie 28th Feb) whereas the other library/code is treating "30th Feb" as 2nd March (since that is technically two days past the 28th, which the 30th would also be). 看起来Jodatime已经“向下舍入”以给出本月的最大有效日期(即2月28日),而其他图书馆/代码将“2月30日”视为3月2日(从技术上讲,这是28日后的两天,其中第30个也是)。

Both are valid assumptions for handling dates IMHO and are a good lesson in why date handling is hard. 两者都是处理日期恕我直言的有效假设,并且是一个很好的教训,为什么日期处理很难。 You'll need to be explicit about which convention you want to follow and you may have to code your assertions to follow Jodatime's conventions. 您需要明确要遵循哪种约定,并且可能必须编写断言以遵循Jodatime的约定。

See: DateTime::plusMonths(int) 请参阅: DateTime :: plusMonths(int)

Returns a copy of this datetime plus the specified number of months. 返回此日期时间的副本加上指定的月数。

The calculation will do its best to only change the month field retaining the same day of month. 计算将尽力仅更改保留当月同一天的月份字段。 However, in certain circumstances, it may be necessary to alter smaller fields. 但是,在某些情况下,可能需要更改较小的字段。 For example, 2007-03-31 plus one month cannot result in 2007-04-31, so the day of month is adjusted to 2007-04-30. 例如,2007-03-31加上一个月不能导致2007-04-31,所以将月份的日期调整为2007-04-30。

So, 30 Sept 2016 + 5 months = 28 Feb 2017 (according to Joda's logic) and it is not a bug 所以, 2016年9月30日 + 5个月= 2017年2月28日 (根据Joda的逻辑),这不是一个错误

Here is sample code for adding months to given calendar date 以下是将月份添加到给定日历日期的示例代码

public class Demo {

 // create a calendar
 Calendar cal = Calendar.getInstance()

// print current date
 System.out.println("The current date is : " + cal.getTime());

 // add 1 months from the calendar
 cal.add(Calendar.MONTH, 1);

}

FYR How to add one month to a date and get the same day FYR 如何将一个月添加到某个日期并获得同一天

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试解析实际日期时出现错误的日期 - Getting wrong date when I am trying to parse actual date 从 UTC 时间戳转换时,为什么我总是收到错误的日期/时间? - Why do I keep getting the wrong date/time when converting from a UTC timestamp? ZonedDateTime 返回错误的日期月数直到另一个结束日期 - ZonedDateTime returns wrong amount of months for a date until another end date 有没有一种方法可以在日期中添加月份以获取以下期望值 - Is there a method to add months to a date fetching the below expected 如何将月份数添加到特定日期 - How to add number of months to a specific date 如何在没有日历的情况下将月份添加到日期? - how to add months to the date without Calendar? 为什么我在某个日期的星期几错了一天? - Why i'm i getting the wrong day of the week for a certain date? 我想获取该月的最后一个日期并添加 n 个月,但日历实例在 Java 中只需要 30 天 - I want to get last date of the month and add n months to it but calendar instance is taking only 30 days in Java Java - 我如何将日期作为输入并能够添加/减去其天/月/年 - Java - how do I take date as input and be able to add/subtract its days/months/years Java:如何在不使用日历的情况下操作日期以添加天数或月数? - Java: How can I manipulate date to add days or months without using Calendar?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM