Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subtracting a DateTime from a DateTime in scala

Tags:

scala

jodatime

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:


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.

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?

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?

Thanks, John

like image 574
jxstanford Avatar asked Nov 25 '10 01:11

jxstanford


1 Answers

Period formatting is done by the PeriodFormatter class. 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:


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.

like image 104
Arjan Blokzijl Avatar answered Sep 22 '22 05:09

Arjan Blokzijl