Scala Get Current Date

If you have worked in the C++ environment before, you must have known that all object-oriented programming languages are based on instances to perform several tasks. Similarly, the Scala programming language is also an object-oriented environment and thus uses many classes. One of its classes is the Calendar class, which is popular among programmers to get the specific region’s date and time in a moment. If you don’t have any prior knowledge of getting the date and time in the Scala language in different ways, this article will provide you with great assistance in this regard. So, let’s start fresh.

Example # 01:

Getting started with our first example, we have been listing the current working directory contents via the “ls” single-word query. We don’t have any scala files in the directory that can be used for code. So, we have to generate one, by the use of simple a “touch” instruction.

In our first example of Scala programming, we will be taking a look at the utilization of the gettime() function of a Calendar class to display the current date and time. For this, we must import the “java.util.Calendar” class library in our first code line. We have created an object named “Get” on the second line and added a main() function definition to it. The main() function execution is initiated from the declaration of a variable “d” that has been taking the current DateTime instance from the Calendar class using the “getInstance()” function.

After taking the instance, the variable “d” has been utilized to call the getTime() function of the Calendar class. This function returns and saves the current date and time of a specific time zone to a new variable named “DateTime” in a standard display format. After this, the println() function has been cast off to display the value inside the “DateTime” variable, i.e., current date and time of a specific time zone region. The main() function and object “get” are completed here. So, we need to save this code first before the compilation.

Just like many other programming languages, Scala has its own compiler to be used for the compilation of a Scala program on Linux. So, we have tried the “scalac” compiler along with the name of a code file to compile it. The object file has been created with its object name “Get”. So, we have been using its object name within the “scala” execution instruction to run the compiled code. The output has been clearly showing the current date and time on our screen.

Example # 02:

The first example was using the getTime() function to display the current date along with the time on our console screen but we are only searching for ways to display the date only. Thus, we came up with another method, i.e., the use of the LocalDate() function of a Calendar class. This will be a very short and easy-to-do example. Therefore, we have updated the same old scala file. We imported the very same “java.util.Calendar” class and created an object “Get” via the keyword “object”. Within this object, the very same main() function definition has been used with a lot of changes.

It only contains a single println() function statement that calls the LocalDate() function with the now() parameter to get the current local date on our screen display according to the current region. It is as simple as it has been shown in the image.

Let’s save this updated short Scala program in the same file and compile it again on the console with the Scala compiler. The updated object file has been used in the Scala execution query to display its result. It only shows the date of a current specific region that has been utilized.

Example # 03:

Here comes our last example for this tutorial to display the current date and time on our console application screen. But we have been using other functions in this illustration to get the specified format on our screen. So, we have started this example code with the same “java.util.Calendar” line to import the Calendar class instance here. After this, we have been importing the SimpleDateFormat function from the java.text Java module.

We have created an object “Get” as displayed and added the definition of a main() function in it to start the execution. Within the main() function execution, we have created two values, form1 and form2, separately, using the SimpleDateFormat() class object. Both values contain quite different formats for displaying the date. At the very next line, the value “obj” has been initialized by using the getInstance() function of the Calendar class. The println() function is here to get the current date and time via the getTime() function and display it right at the moment.

After this, we have created two more variable values, “Date1” and “Date2” to convert the current DateTime format into a specified format via the form1 and form2 variable objects, i.e., in the format() function call. In the last two lines of code, we have been using the println() function statement to display the newly formatted new dates on the console screen through the use of Date1 and Date2 variable values. Let’s just quickly save our Scala script before its compilation.

After compiling the Scala code file, we executed the “object” name within the “scala” instruction to run the compiled code. The output is clearly showing the Current Date and Time on our console screen first, and then it has been showing two different formats of the current Date.


Our article’s introduction is a summary of how important object-oriented programming languages like Scala are in our daily lives. Along with that, it shows that you can get the Date and Time within a few seconds with a bunch of code lines. Thus, we have explained 3 different methods in Scala programming to display the Date and Time of a specific timezone. We have also discussed a way to alter the format of the current date using the SimpleDateFormat() function.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.