Date & Time in Scala

Dealing with Date/Time in Scala

Xiaodong DENG, http://XD-DENG.com

Contents



1. System.currentTimeMillis & Thread.sleep

System.currentTimeMillis is a method from Java. It returns the current time in milliseconds [1].

Thread.sleep is also a method from Java. It causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers [2].

In [1]:
val t1 = System.currentTimeMillis
Thread.sleep(500) // pause for 0.5 seconds (500 millisesonds)
val t2 = System.currentTimeMillis
Out[1]:
t1: Long = 1527229092378L
t2: Long = 1527229092880L
In [2]:
t2 - t1
Out[2]:
res1: Long = 502L

2. java.util.Calendar: Get Current Date & Time

In [3]:
import java.util.Calendar

val now = Calendar.getInstance.getTime
val nowInMillis = Calendar.getInstance.getTimeInMillis
Out[3]:
import java.util.Calendar


now: java.util.Date = Fri May 25 14:18:15 SGT 2018
nowInMillis: Long = 1527229095739L
In [4]:
println(now.getClass)
println(nowInMillis.getClass)
class java.util.Date
long
In [5]:
println(now.toString)
println(now.toInstant)
Fri May 25 14:18:15 SGT 2018
2018-05-25T06:18:15.739Z

3. java.text.SimpleDateFormat: Convert Date/Time to Desired Format

It's common that we need to customize the format of date/time. For this purpose, we need tools like strftime in Python. In Scala, we can use java.text.SimpleDateFormat [3].

Using java.text.SimpleDateFormat, we can create date/time formatters, then apply them on the java.util.Date objects from Calendar.getInstance.getTime, like the one (now) we specified in the last section.

In [6]:
import java.text.SimpleDateFormat

// create the date/time formatters
val hour12Format = new SimpleDateFormat("hh")
val hour24Format = new SimpleDateFormat("HH")
val minuteFormat = new SimpleDateFormat("mm")
val amPmFormat = new SimpleDateFormat("a")
val dateFormat = new SimpleDateFormat("yyyy-MM-dd")
Out[6]:
import java.text.SimpleDateFormat

// create the date/time formatters

hour12Format: java.text.SimpleDateFormat = java.text.SimpleDateFormat@d00
hour24Format: java.text.SimpleDateFormat = java.text.SimpleDateFormat@900
minuteFormat: java.text.SimpleDateFormat = java.text.SimpleDateFormat@da0
amPmFormat: java.text.SimpleDateFormat = java.text.SimpleDateFormat@61
dateFormat: java.text.SimpleDateFormat = java.text.SimpleDateFormat@f67a0200
In [7]:
println(dateFormat.getClass)
class java.text.SimpleDateFormat
In [8]:
println(now)

println(hour12Format.format(now) )
println(hour24Format.format(now) )
println(minuteFormat.format(now))
println(amPmFormat.format(now)  )
println(dateFormat.format(now))
Fri May 25 14:18:15 SGT 2018
02
14
18
PM
2018-05-25

4. Convert Strings into Date/Time

Similarly, we may need to convert strings into Date/Time. The tool we need is the same, java.text.SimpleDateFormat. We simply need its another method, .parse [4].

Using the formatter specified in the last section as example,

In [9]:
dateFormat.parse("2017-01-01")
Out[9]:
res8: java.util.Date = Sun Jan 01 00:00:00 SGT 2017

We can specify another formatter in which we have richer information,

In [10]:
val full_dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
Out[10]:
full_dateFormat: SimpleDateFormat = java.text.SimpleDateFormat@4f76f1a0
In [11]:
print(full_dateFormat.parse("2018-12-26 17:15:00"))
Wed Dec 26 17:15:00 SGT 2018