A Scala current date and time example

Scala date/time FAQ: How do I get the current date and time in Scala?

In this article I’ll show a couple of different ways to get the current date and time in Scala.

Back to top

Getting the current minute and hour

The simplest way to get the “current minute” in Scala is to use this approach with the java.util.Calendar class:

val now = Calendar.getInstance()
val currentMinute = now.get(Calendar.MINUTE)

This approach returns an Int value like 24, meaning “24 minutes after the hour.” As you can imagine, you can use other Calendar class static fields with the get() method to get the current minute, current hour, etc.

This example shows how to get the current hour:

val now = Calendar.getInstance()
val currentHour = now.get(Calendar.HOUR_OF_DAY)

Note that Calendar.HOUR_OF_DAY returns a value from 0 to 23, so if it’s 1:35 p.m., this returns the value 13.

Back to top

Getting the current time in Scala using SimpleDateFormat

Another way to get the current time in Scala, involves using Java’s SimpleDateFormat class. Here’s a Scala method that shows how to get the current minute, and then determines whether the current minute is divisible by 5:

def onTheFives: Boolean = {
  val now = Calendar.getInstance().getTime()
  val minuteFormat = new SimpleDateFormat("mm")
  val currentMinuteAsString = minuteFormat.format(now)
  try {
    val currentMinute = Integer.parseInt(currentMinuteAsString)
    if (currentMinute % 5 == 0) return true
    else return false
  } catch {
    case _ => return false
  }
}

As you can see, you get the current time in Scala with this line of code:

val now = Calendar.getInstance().getTime()

This uses the Java Calendar class, which you'll have to import into your Scala application.

I later get the “current minute” from the current time object using the Java SimpleDateFormat class using these lines of code:

val minuteFormat = new SimpleDateFormat("mm")
val currentMinuteAsString = minuteFormat.format(now)

One problem with this approach is that the minute comes back as a String, which can be something like "01". Because I’m trying to get my computer to speak the current time to me, I don’t want that leading zero, so I convert the String to an Int using the code shown in the try/catch brackets.

Back to top

Getting the current hour using SimpleDateFormat

On a related note, if you want to get the current hour in Scala, you can use this very similar method:

def getCurrentHour: String = {
  val now = Calendar.getInstance().getTime()
  val hourFormat = new SimpleDateFormat("hh")
  try {
    // returns something like "01" if i just return at this point, so cast it to
    // an int, then back to a string (or return the leading '0' if you prefer)
    val currentHour = Integer.parseInt(hourFormat.format(now))
    return "" + currentHour
  } catch {
    // TODO return Some/None/Whatever
    case _ => return "0"
  }
  return hourFormat.format(now)
}

This code returns the current hour as a String. (I’m sure there are probably better ways to write this, but my writing time for today is up, so I’ll have to leave the code like this for now.)

Back to top

More Scala SimpleDateFormat examples

While I’m in the neighborhood, I’ll share the following Scala code, which shows a few more SimpleDateFormat examples:

val today = Calendar.getInstance().getTime()
    
// create the date/time formatters
val minuteFormat = new SimpleDateFormat("mm")
val hourFormat = new SimpleDateFormat("hh")
val amPmFormat = new SimpleDateFormat("a")

val currentHour = hourFormat.format(today)      // 12
val currentMinute = minuteFormat.format(today)  // 29
val amOrPm = amPmFormat.format(today)           // PM
Back to top

Summary: Getting the current date/time in Scala

In summary, if you need to get the current date or current time in Scala, I hope these examples are helpful.

Back to top
Permalink

Just thought I'd say that one can get around the need for using SimpleDateFormat by using the instance of Calendar.

def onTheFives :Boolean = {
val now = Calendar.getInstance()
val currentMinute = now.get(Calendar.MINUTE)
return (currentMinute % 5 == 0)
}

etc.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.