How to Get the Current Date and Time in Scala
- Use the Nscala-Time Library to Get the Current Date and Time in Scala
-
Use the Java
Calendar
Class to Get the Current Date and Time in Scala
This article will demonstrate how to get the current date and time in Scala. Scala is a JVM-based general-purpose programming language, and just like any general-purpose programming here, the date and time have a very important role.
Unlike Java, Scala does not have native date-time libraries. But we have some libraries in Scala, which are the wrapper around the well-known Java libraries like Joda-Time.
Using these libraries, we can get the elegance of Scala’s expressive coding style along with the power of underlying Java libraries. We can also use the native Java date and time libraries in Scala.
Use the Nscala-Time Library to Get the Current Date and Time in Scala
In Scala, the Nscala-Time library is a wrapper library around Joda-Time. This gives many operators to perform date-time arithmetic as well.
To use it we have to import com.github.nscala_time.time.Imports._
and org.joda.time.Days
in our code file.
Let’s look at the example below:
val start:DateTime = DateTime.now()
val end:DateTime = x + (1.hours + 10.minutes + 5.seconds)
val elapsed:Interval = start to end
println(elapsed.toPeriod.toString(PeriodFormat.getDefault))
Use the Java Calendar
Class to Get the Current Date and Time in Scala
Using the classical Java Calendar
class, we can get the current date and time in Scala. It’s an Abstract
class used to get the current date and time; this same class can also be used in Scala.
Get the Current Time Only Using the getInstance()
Method
Example code:
import java.util.Calendar;
object myClass {
def main(args: Array[String])
{
var date = Calendar.getInstance()
var curr_Minute = date.get(Calendar.MINUTE)
var curr_Hour = date.get(Calendar.HOUR_OF_DAY)
if(curr_Hour > 12)
{
curr_Hour = curr_Hour % 12
println("Current time is "+curr_Hour+":"+curr_Minute+" PM")
}
else{
println("Current time is "+curr_Hour+":"+curr_Minute+" AM")
}
}
}
Output:
Current time is 11:12 AM
Explanation:
In the above code, the Calendar
class’s getInstance()
method is used to find the time. We have used calendar.MINUTE
and calendar.HOUR_OF_DAY
and stored the results in the variables curr_Minute
and curr_Hour
, respectively.
The method calendar.HOUR_OF_DAY
returned the output in 24-hour format, and we used conditional statements to convert it into a 12-hour format.
Get the Full Current Date and Time Using the getTime()
Method
Example code:
import java.util.Calendar;
object myClass {
def main(args: Array[String]) {
var date = Calendar.getInstance()
var currentHour = date.getTime()
println("Current date and time is "+currentHour)
}
}
Output:
Current date and time is Fri Jun 03 11:25:16 GMT 2022
Explanation:
In the above code, we have used the inbuilt method of the Calendar
class, the getTime()
method. It returns the current date and time in MM DD time UTC YYYY
.
Get the Current Date Only Using LocalDate
Example code:
import java.util.Calendar;
object myClass {
def main(args: Array[String]) {
println(java.time.LocalDate.now)
}
}
Output:
2022-06-03
Explanation:
We have used the LocalDate.now
to get the current date. We can also use SimpleDateFormat
class along with the Calendar
class to format the date into the specific format of our choice.
We have to import java.text.SimpleDateFormat
in our code to use it.
Get the Formatted Current Date Using the getTime()
Method
Example code:
import java.util.Calendar;
import java.text.SimpleDateFormat;
object myClass
{
def main(args: Array[String])
{
val ourform = new SimpleDateFormat("dd / MM / yy");
val temp = Calendar.getInstance();
println("current Date is : " + temp.getTime());
val formatted_Date = ourform.format(temp.getTime());
println("Formatted date is: "+formatted_Date);
}
}
Output:
current Date is : Fri Jun 03 11:45:35 GMT 2022
Formatted date is: 03 / 06 / 22