Scala FAQ: How do I get the current month as an integer or as a string in Scala?
Solution
In the following examples I show how to get the current month in Scala as a number (integer) and as a string, both as the month abbreviation and the full name of the month. In all of these examples I use Java classes like Calendar and SimpleDateFormat.
Get the current month as an integer number
With the Java Calendar
class the months are zero-based, so January=0, February=1, etc. Here’s how to get the current month as an integer value:
import java.util.Calendar
val monthAsInt = Calendar.getInstance.get(Calendar.MONTH)
As of this writing on March 15, 2018, here’s what that looks like in the Scala REPL:
scala> val monthAsInt = Calendar.getInstance.get(Calendar.MONTH)
monthAsInt: Int = 2
With Calendar
being zero-based, the month of March comes back as the number 2
.
How to get the current month as an abbreviated string
Here’s how you get the current month as a three-character string. First, the necessary imports:
import java.util.Calendar
import java.text.SimpleDateFormat
Here’s the step by step version of the solution:
val cal = Calendar.getInstance
val dateTime = cal.getTime
val dateFormat = new SimpleDateFormat("MMM")
val mon = dateFormat.format(dateTime)
Here’s the same solution with all code on one line:
val mon = new SimpleDateFormat("MMM").format(Calendar.getInstance.getTime)
To demonstrate how this works, here’s the long form of the solution in the Scala REPL:
scala> val cal = Calendar.getInstance
cal: java.util.Calendar = java.util.GregorianCalendar[time=1521153660718...
scala> val dateTime = cal.getTime
dateTime: java.util.Date = Thu Mar 15 16:41:00 MDT 2018
scala> val dateFormat = new SimpleDateFormat("MMM")
dateFormat: java.text.SimpleDateFormat = java.text.SimpleDateFormat@12aad
scala> val mon = dateFormat.format(dateTime)
mon: String = Mar
As shown, the month of March prints as “Mar,” so you may want to convert the result to upper-case or lower-case:
scala> val mon = new SimpleDateFormat("MMM").format(cal.getTime()).toUpperCase
mon: String = MAR
scala> val mon = new SimpleDateFormat("MMM").format(cal.getTime()).toLowerCase
mon: String = mar
How to get the full month name
To get the full month name in Scala — i.e., January, February, March, etc. — use one of the following approaches. First, I found that you can get the full month name as a string without using SimpleDateFormat
:
val cal = Calendar.getInstance
val monthName = cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault)
Here’s what that looks like in the REPL:
scala> val cal = Calendar.getInstance
cal: java.util.Calendar = java.util.GregorianCalendar[time=1521154650578...
scala> val monthName = cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault)
monthName: String = March
Again use toUpperCase
and toLowerCase
there as needed.
To be consistent with the earlier solutions you can use the combination of Calendar
and SimpleDateFormat
like this:
import java.util.Calendar
import java.text.SimpleDateFormat
import java.util.Locale
val dateTime = Calendar.getInstance.getTime
val dateFormat = new SimpleDateFormat("LLLL")
dateFormat.format(dateTime)
Here’s what that looks like in the Scala REPL:
scala> val dateTime = Calendar.getInstance.getTime
dateTime: java.util.Date = Thu Mar 15 17:03:01 MDT 2018
scala> val dateFormat = new SimpleDateFormat("LLLL")
dateFormat: java.text.SimpleDateFormat = java.text.SimpleDateFormat@23b300
scala> dateFormat.format(dateTime)
res1: String = March
A note about SimpleDateFormat and Locale
When using SimpleDateFormat
you may also want (or need) to specify the “locale,” like this:
val dateFormat = new SimpleDateFormat("LLLL", Locale.getDefault)
Per the SimpleDateFormat Javadoc:
- If you don’t specify the locale, “Constructs a SimpleDateFormat using the default pattern and date format symbols for the default FORMAT locale.”
- If you do specify the locale, “Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the given locale.”
I’ve never done too much work with international dates and formats, but it seems like whenever I leave the Locale
out of a solution, people internationally say that this can be a problem.
Summary
In summary, if you needed to see how to get the current month in Scala as an integer value, an abbreviated string value, or as the full month string name, I hope these examples are helpful.