获取乔达时间的完整月份和剩余天数

发布于 2025-01-16 22:58:19 字数 696 浏览 3 评论 0原文

我有以下代码片段,我试图用它来获取特定日期和剩余天数之间的总月份。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文