Subtracting DateTime from DateTime in scala

I am relatively new to both scala and jodatime, but I was impressed by both of them. I am trying to figure out if there is a more elegant way to do some date arithmetic. Here's the 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 put it in MongoDB, and I'm not sure how to serialize the duration or period of a joda. If anyone knows that I am very grateful for the answer.

In either case, the calcCloseTime and calcOpenTime methods return DateTime objects. Converting them to Instants is the best way to find the difference. Is there a better way?

Another question: when hours, minutes or seconds are a single digit, the resulting string is not filled with zero. Is there an easy way to make this line look like HH: MM: SS?

Thanks, John

+6
scala jodatime
source share
3 answers

Period formatting is performed by the PeriodFormatter class. You can use it by default or create your own PeriodFormatterBuilder . Another code may be required, as you may like to install this constructor correctly, but you can use it, for example, like this:

 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 also be aware that daily crossings do not count:

 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 

therefore, if you also need these hands, you also need to add the necessary fields (days, weeks, years) to the formatter.

+8
source share

You might want to take a look at Jorge Ortiz's wrapper for Joda-Time, scala-time for something better to work with in Scala.

Then you should use something like

 (calcOpenTime to calcCloseTime).millis 
+7
source share

Does this link help?

How to calculate the difference between two dates?

This question has more than one answer! If you just need the number of whole days between two dates, 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 in the Days class were designed to work effectively with the JDK5 static import tool.

If you want to calculate the number of days, weeks, months, and years between two dates, you will need a period. By default, this will split the difference between the two dates into parts, for example, “1 month, 2 weeks, 4 days and 7 hours”.

 Period p = new Period(startDate, endDate); 

You can control which fields are retrieved using PeriodType.

 Period p = new Period(startDate, endDate, PeriodType.yearMonthDay()); 

In this example, no weeks or time fields are returned, so the previous example becomes "1 month and 18 days."

+1
source share

All Articles