Scala date/time FAQ: How do I get the current date and time in Scala?
Solution: In this article I’ll show a couple of different ways to get the current date and time in Scala.
Update!
This article is a little old and I don’t have time to update it at the moment, so if you’re interested in the newest solutions for the JDK/JVM 8 platform and newer — i.e., Java 8, Java 11, 14, 17, etc. — please see these NEW solutions:
- Scala 3: Creating new date and time instances
- Scala 3 dates: How to format dates
- Scala 3 dates: How to parse strings into dates
- Scala 3 dates: How to calculate the difference between two dates
For older Java and Scala date/time examples, see below ...
Scala: Getting the current minute and hour
Note: This is the old solution that I used prior to the big date/time changes in Java 8.
A simple 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
.
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.
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.)
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
this post is sponsored by my books: | |||
#1 New Release |
FP Best Seller |
Learn Scala 3 |
Learn FP Fast |
Scala 3 Date and Time examples (and Java 8+)
As a quick update, in the 2nd Edition of the Scala Cookbook I share the following Scala/Java/JVM date and time recipes:
- Scala 3: Creating new date and time instances
- Scala 3 dates: How to format dates
- Scala 3 dates: How to parse strings into dates
- Scala 3 dates: How to calculate the difference between two dates
Those date/time recipes will work with Java 8, Java 11, Java 14, etc.
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.