0

I'm trying to calculate the time from various dates since a specific period start date. Calculating the dates since was fine and easy, but I also want the function to return -1 if the dates between them is negative. I'm not sure a clean way to do this? I've got:

def getTimeBetween(date1: String, date2: String, format: String, dtype: String): Int = { dtype match { case "fixed" => { if (date1 == null) throw new Exception("Specify date1") else Days.daysBetween(date2, date1).getDays() } case _ => throw new Exception("Enter a valid type") } } 

I'm pretty new to Scala so I don't know all the syntax well yet. I'm more used to Java/C++, so I thought next if/else statements would work but I've realized it's not the way to go.

Thanks.

4
  • Firstly, type is a keyword in scala so you cannot use it as a variable. Secondly, can you use a library like joda.time or are you restricted to primitive types? Commented May 19, 2015 at 16:28
  • That's not exactly what i named everything. I'm doing this as an intern and didn't want to put my actual code up. I am using the joda.time library Commented May 19, 2015 at 16:31
  • Are date1 and date2 actually Strings here or do you have them as DateTime types? Commented May 19, 2015 at 16:37
  • I'm using primitive types because this is with hadoop. I can get the days between just fine. I just pass in the date string and the format of the date. Commented May 19, 2015 at 16:37

2 Answers 2

1

A definite improvement would be to not throw runtime exceptions and instead rely on a standard Scala construct, Try comes to mind here.

import scala.util.{ Success, Failure, Try } import org.joda.time.Days def getTimeBetween(date1: String, date2: String, format: String, dtype: String): Try[Int] = { dtype match { case "fixed" => { Try(Days.daysBetween(date2, date1).getDays) match { case Success(days) if days >= 0 => Success(days) case Success(_) => Success(-1) case Failure(t) => Failure(t) } } case _ => Failure(new IllegalArgumentException) } } 

This method will return Success(#days) with the number of days between if they're positive, or Success(-1) if they're negative.

The Try wrapper around the daysBetween function will catch any exceptions that might be thrown during parsing, such as illegal formats or null values in the parameters. Any runtime exception will result in a Failure(t) with t being the exception caught.

There's a lot of other ways to approach this, like the Either type, or using Option and the scala.util.catching function.

Edit:

Since you left it out of your example I did also, but for the daysBetween method to compile you'll need to parse the strings into DateTime objects, using something like DateTime.parse(date1) etc.

Sign up to request clarification or add additional context in comments.

5 Comments

Given that date1 and date2 are strings, isn't some explicit conversion necessary?
This is exactly what I was looking for. I was trying various things with match but couldn't find something like Try/Success/Failure. My only question though is when returning values, can I just be like if days >= 0 => days or does returning Success(days) have any extra value?
Notice the return type of the method, it's Try[Int]. So you can't return just days since that is an integer.
Yeah but if I just keep the return type as Int, is there anything inherently wrong with that? Does returning Try[Int] have any extra value?
Yes, since any exceptions get caught in the Failure type, instead of being thrown at runtime. Failure and Success extend Try, so to use them you must return a Try[T] in your method. Read some more over here: danielwestheide.com/blog/2012/12/26/…
0

Assuming your method returns a negative integer and you want it to return -1 instead of all negative values you could simply put it in an auxilary variable and check

val res = Days.daysBetween(date2, date1).getDays() if (res <0) -1 else res 

A more compact (and argueably less clear method) only for Int would be using max

Days.daysBetween(date2, date1).getDays().max(-1) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.