Scala program to print the current date and time using Date class

Here, we are going to learn how to print the current date and time using Date class in Scala programming language?
Submitted by Nidhi, on May 30, 2021 [Last updated : March 11, 2023]

Scala - Current Date/Time using Date Class

Here, we will create an object of the Date class. The Date class is available in the "java.util" package. Then we will print the current time on the console screen.

Scala code to print the current date and time using Date class

The source code to print the current date and time using the Date class is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to print the current date and time
// using Date class

import java.util.Date;

object Sample {
  def main(args: Array[String]) {
    var dateObj = new Date();
    println("Current and time: \n" + dateObj);
  }
}

Output

Current and time: 
Sun May 30 13:19:55 GMT 2021

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 an object dateObj of the Date class and then printed the current time using the dateObj object on the console screen.

Scala Date & Time Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.