从 Scala 中的 DateTime 中减去 DateTime
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4272957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
subtracting a DateTime from a DateTime in scala
提问by jxstanford
I'm relatively new to both scala and jodatime, but have been pretty impressed with both. I'm trying to figure out if there is a more elegant way to do some date arithmetic. Here's a method:
我对 scala 和 jodatime 都比较陌生,但对两者都印象深刻。我想弄清楚是否有更优雅的方法来进行一些日期算术。这是一个方法:
private def calcDuration() : String = {
val p = new Period(calcCloseTime.toInstant.getMillis - calcOpenTime.toInstant.getMillis)
val s : String = p.getHours.toString + ":" + p.getMinutes.toString +
":" + p.getSeconds.toString
return s
}
I convert everything to a string because I am putting it into a MongoDB and I'm not sure how to serialize a joda Duration or Period. If someone knows that I would really appreciate the answer.
我将所有内容都转换为字符串,因为我将它放入 MongoDB 中,但我不确定如何序列化 joda Duration 或 Period。如果有人知道我真的很感激答案。
Anyway, the calcCloseTime and calcOpenTime methods return DateTime objects. Converting them to Instants is the best way I found to get the difference. Is there a better way?
无论如何, calcCloseTime 和 calcOpenTime 方法返回 DateTime 对象。将它们转换为 Instants 是我发现区别的最佳方式。有没有更好的办法?
Another side question: When the hours, minutes or seconds are single digit, the resulting string is not zero filled. Is there a straightforward way to make that string look like HH:MM:SS?
另一个问题:当小时、分钟或秒是个位数时,生成的字符串不是零填充的。有没有一种简单的方法可以使该字符串看起来像 HH:MM:SS?
Thanks, John
谢谢,约翰
回答by Arjan Blokzijl
Periodformatting is done by the PeriodFormatterclass. You can use a default one, or construct your own using PeriodFormatterBuilder. It may take some more code as you might like to set this builder up properly, but you can use it for example like so:
Period格式化由PeriodFormatter类完成。您可以使用默认的,也可以使用PeriodFormatterBuilder构建自己的。由于您可能希望正确设置此构建器,因此可能需要更多代码,但您可以像这样使用它:
scala> import org.joda.time._
import org.joda.time._
scala> import org.joda.time.format._
import org.joda.time.format._
scala> val d1 = new DateTime(2010,1,1,10,5,1,0)
d1: org.joda.time.DateTime = 2010-01-01T10:05:01.000+01:00
scala> val d2 = new DateTime(2010,1,1,13,7,2,0)
d2: org.joda.time.DateTime = 2010-01-01T13:07:02.000+01:00
scala> val p = new Period(d1, d2)
p: org.joda.time.Period = PT3H2M1S
scala> val hms = new PeriodFormatterBuilder() minimumPrintedDigits(2) printZeroAlways() appendHours() appendSeparator(":") appendMinutes() appendSuffix(":") appendSeconds() toFormatter
hms: org.joda.time.format.PeriodFormatter = org.joda.time.format.PeriodFormatter@4d2125
scala> hms print p
res0: java.lang.String = 03:02:01
You should perhaps also be aware that day transitions are not taken into account:
您或许还应该意识到不考虑日间转换:
scala> val p2 = new Period(new LocalDate(2010,1,1), new LocalDate(2010,1,2))
p2: org.joda.time.Period = P1D
scala> hms print p2
res1: java.lang.String = 00:00:00
so if you need to hanldes those as well, you would also need to add the required fields (days, weeks, years maybe) to the formatter.
因此,如果您还需要处理这些,您还需要将所需的字段(可能是天、周、年)添加到格式化程序中。
回答by Jon McAuliffe
You might want to take a look at Jorge Ortiz's wrapper for Joda-Time, scala-timefor something that's a bit nicer to work with in Scala.
您可能想看看 Jorge Ortiz 的 Joda-Time 包装器,scala-time用于在 Scala 中使用更好的东西。
You should then be able to use something like
然后你应该能够使用类似的东西
(calcOpenTime to calcCloseTime).millis
回答by Synesso
Does this linkhelp?
请问此链接帮助?
How do I calculate the difference between two dates?
This question has more than one answer! If you just want the number of whole days between two dates, then you can use the new Days class in version 1.4 of Joda-Time.
Days d = Days.daysBetween(startDate, endDate); int days = d.getDays();This method, and other static methods on the Days class have been designed to operate well with the JDK5 static import facility.
If however you want to calculate the number of days, weeks, months and years between the two dates, then you need a Period By default, this will split the difference between the two datetimes into parts, such as "1 month, 2 weeks, 4 days and 7 hours".
Period p = new Period(startDate, endDate);You can control which fields get extracted using a PeriodType.
Period p = new Period(startDate, endDate, PeriodType.yearMonthDay());This example will return not return any weeks or time fields, thus the previous example becomes "1 month and 18 days".
如何计算两个日期之间的差异?
这个问题有不止一个答案!如果您只想要两个日期之间的整天数,那么您可以使用 Joda-Time 1.4 版中的新 Days 类。
Days d = Days.daysBetween(startDate, endDate); int days = d.getDays();此方法和 Days 类中的其他静态方法旨在与 JDK5 静态导入工具一起运行良好。
但是,如果您想计算两个日期之间的天数、周数、月数和年数,那么您需要一个 Period 默认情况下,这会将两个日期时间之间的差异分成几部分,例如“1 个月、2 周、 4天7小时”。
Period p = new Period(startDate, endDate);您可以使用 PeriodType 控制提取哪些字段。
Period p = new Period(startDate, endDate, PeriodType.yearMonthDay());此示例将返回不返回任何周或时间字段,因此前面的示例变为“1 个月零 18 天”。

