Here, we will create two objects of the Date class. Then we will find the difference between the two dates and print the difference in days on the console screen.
The source code to find the difference between the two dates is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.
// Scala program to find the difference in two dates
import java.util.Date;
object Sample {
def main(args: Array[String]) {
var firstDate = new Date(2021, 5, 10);
var secondDate = new Date(2021, 5, 20);
var ms = secondDate.getTime() - firstDate.getTime();
var days = (ms / (60 * 60 * 24 * 1000));
println("Days: " + days);
}
}
Output:
Days: 10
Explanation:
Here, we used an object-oriented approach to create the program. And, we imported the Date class using the below statement,
import java.util.Date;
Then we created a singleton object Sample and defined the main() function. The main() function is the entry point for the program.
In the main() function, we created two objects of the Date class and then we find the difference between dates and convert the result into days, and print on the console screen.
Program/Source Code:
The source code to find the difference between the two dates is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.
Output:
Explanation:
Here, we used an object-oriented approach to create the program. And, we imported the Date class using the below statement,
Then we created a singleton object Sample and defined the main() function. The main() function is the entry point for the program.
In the main() function, we created two objects of the Date class and then we find the difference between dates and convert the result into days, and print on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer