获取乔达时间的完整月份和剩余天数
我有以下代码片段,我试图用它来获取特定日期和剩余天数之间的总月份。
import com.github.nscala_time.time.Imports._
import org.joda.time.{DateTime, Days, Interval, Weeks, Months}
import scala.math.Integral.Implicits._
val start = new DateTime(2022, 3, 1, 0, 0, 0, 0)
val end = new DateTime(2022, 4, 30, 0, 0, 0, 0)
val days = Days.daysBetween(start, end).getDays + 1
val monthss = Months.monthsBetween(start, end)
val months = Math.abs(monthss.getMonths())
val (monate, remaining)= days /% 30
println(days)
println(monate)
println(remaining)
可以看出,它打印了以下内容:
61
2
1
这是错误的。我知道错误在哪里。由于我总是以 30 作为 mod,它给我的剩余天数为 1,但在这里我预计这是一个完整的 2 个月期间,剩余天数为 0。
关于如何使其正确的任何想法?
I have the following code snippet with which I'm trying to get the total months that are in between a certain date and the remaining as the number of days.
import com.github.nscala_time.time.Imports._
import org.joda.time.{DateTime, Days, Interval, Weeks, Months}
import scala.math.Integral.Implicits._
val start = new DateTime(2022, 3, 1, 0, 0, 0, 0)
val end = new DateTime(2022, 4, 30, 0, 0, 0, 0)
val days = Days.daysBetween(start, end).getDays + 1
val monthss = Months.monthsBetween(start, end)
val months = Math.abs(monthss.getMonths())
val (monate, remaining)= days /% 30
println(days)
println(monate)
println(remaining)
As it can be seen that it prints the following:
61
2
1
Which is wrong. I know where the mistake is. As I'm always taking 30 as the mod, it gives me a remaining number of days as 1, but here I expect that it is a full 2 month period with a remaining day of 0.
Any ideas as to how to get it correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论