ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Scala program to set datetime using setTime() method

Here, we are going to learn how to set datetime using setTime() method in Scala programming language?
Submitted by Nidhi, on June 01, 2021

Problem Solution:

Here, we will create an object of the Date class. Then we will set the date-time using setTime(). The setTime() method accepts date time in milliseconds. After that, we will print the updated DateTime on the console screen.

Program/Source Code:

The source code to set date-time using the setTime() method is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to set datetime using setTime() method

import java.util.Date;

object Sample {
  def main(args: Array[String]) {
    var date = new Date();

    System.out.println("Date before setTime() method:\n" + date);
    date.setTime(304687433443L);
    System.out.println("Date after setTime() method:\n" + date);
  }
}

Output:

Date before setTime() method:
Tue Jun 01 05:14:18 GMT 2021
Date after setTime() method:
Tue Aug 28 11:23:53 GMT 1979

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 of the Date class, and then we set the date-time into the created object using the setTime() method. After that, we printed the updated date on the console screen.

Scala Date & Time Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.