Updated April 12, 2023
Definition of Scala Date Function
In scala we do not have any date-time library or function available, we can use java data time, calendar, Date library to deal with dates in Scala. By using these functions, we can convert our date to a string and vice versa, also we can format our date. By using this function, we can get the current date and time, location, etc on scala. This function can also return us the current timestamp and many more. We just need to import the packages in order to use date functions in scala. In the coming section, we will discuss more the date function and their usages in detail.
Syntax:
As earlier explained we do not have any date function available in scala but still, we can use the java library to deal with this in scala. Let’s have a look at the syntax how to use them in programming see below;
1)
import java.time.LocalDate
2)
import java.time.format.DateTimeFormatter
var variable_name = LocalDate.parse(date_as_string, DateTimeFormatter.ofPattern(Your_pattern))
As you can see in the above syntax we are using parse() function from date library which can be used to convert the date object into any specified format and pattern, But in order to use this, we have to import the java libraries in the program.
How does date function work in Scala?
As now we know that there is no date function available in Scala, we have to use the java library to deal with them in scala. Here in this section, we are trying to use the ‘DateTime’ library from java. This library provides various function which are easily used to deal with date in Scala. We will see how to import this library into our existing program and how we can use these functions to get the date object in scala. Also by using these functions, we can convert the date object into a string and vice versa. Also, we can convert them to any pattern and format as well. Let’s talk about some of the methods which are available in Date library see below;
1) import java.util.Date: Include this library into your program at the start because we do not have any specific way to deal with dates in Scala. We are using the java library for this. Inside this package, we have so many functions available which can be used to deal with dates. Let’s first discuss the constructor of this class see below;
- Date(): This is used to get the current date and time. We can just use this with the ‘new’ keyword.
e.g. :
var d1 = new Date();
- Date(String s): In this, we can pass the data as the string parameter and it will convert that string into a date object. See below for its usage in Scala;
e.g. :
var d1 = new Date("11/08/2020")
- Date(int year, int month, int date): This takes three parameters as the input here. At first, we can pass the year, the second parameter accepts month and the last parameter accepts the date. After this, it will return us the date object in Scala. Let’s see its usage in Scala see below;
e.g. :
var d1 = new Date(2020, 09, 03)
- Date(long milliseconds): In this, we can pass the milliseconds then according to this it will calculate the date and return us. Let’s see its usage in Scala see below;
e.g. :
var d1 = new Date(89808989)
- Date(int year, int month, int date, int hrs, int min): This constructor object takes five parameters and the basis of it will create the date object. Inside this, we can pass year, month, date, hours, and minutes respectively as the input param. Let’s see its usage in scala see below;
e.g. :
var d1 = new Date(2020, 08, 19, 13, 50)
- Date(int year, int month, int date, int hrs, int min, int sec): This constructor object takes five parameters and the basis of it will create the date object. Inside this, we can pass year, month, date, hours, minutes, and seconds respectively as the input param. Let’s see its usage in scala see below;
e.g. :
var d1 = new Date(2020, 08, 19, 13, 50, 00)
Examples
In this example we are creating the date object it will give us today date.
Example #1
Code:
import java.util.Date
object Main extends App{
// Your code here!
println("Demo for date function in scala !!!")
var d1 = new Date()
println(d1);
}
Output:
Example #2
In this example, we are creating date by using year month, and date passing inside the constructor in scala.
Code:
import java.util.Date
object Main extends App{
// Your code here!
println("Demo for date function in scala !!!")
var d1 = new Date(2020, 08, 19)
var d2 = new Date(2019, 07, 18)
var d3 = new Date(2018, 06, 17)
var d4 = new Date(2017, 05, 16)
var d5 = new Date(2016, 04, 15)
println("result for date one is ::: ")
println(d1);
println("result for date two is ::: ")
println(d2);
println("result for date three is ::: ")
println(d3);
println("result for date four is ::: ")
println(d4);
println("result for date five is ::: ")
println(d5);
}
Output:
Example #3
In this example, we are creating date by using year month, date, hour, and minute passing inside the constructor in Scala.
Code:
import java.util.Date
object Main extends App{
// Your code here!
println("Demo for date function in scala !!!")
var d1 = new Date(2020, 08, 19, 01, 20)
var d2 = new Date(2019, 07, 18, 10, 30)
var d3 = new Date(2018, 06, 17, 13, 30)
var d4 = new Date(2017, 05, 16, 14, 20)
var d5 = new Date(2016, 04, 15, 20, 22)
println("result for date one is ::: ")
println(d1);
println("result for date two is ::: ")
println(d2);
println("result for date three is ::: ")
println(d3);
println("result for date four is ::: ")
println(d4);
println("result for date five is ::: ")
println(d5);
}
Output:
Conclusion
So to use date in scala we can use java library by importing packages. After this we can use its function to deal with date and time in scala, we can parse, format, and create the date from string object as well.
Recommended Articles
We hope that this EDUCBA information on “Scala Date Function” was beneficial to you. You can view EDUCBA’s recommended articles for more information.