Scala program to demonstrate the before() method of Date class

Here, we are going to demonstrate the before() method of Date class in Scala programming language.
Submitted by Nidhi, on June 01, 2021 [Last updated : March 11, 2023]

Scala - Date.before() Method Example

Here, we will create objects of the Date class and compare dates using the before() method. The before() method returns true if the specified date object is greater than the current date object. Otherwise, it returns false.

Scala code to demonstrate the example of Date.before() method

The source code to demonstrate the before() method of the Date class is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to demonstrate the
// before() method of Date class

import java.util.Date;

object Sample {
  def main(args: Array[String]) {
    var date1 = new Date(2021, 5, 10);
    var date2 = new Date(2021, 5, 11);
    var date3 = new Date(2021, 5, 9);

    var result: Boolean = false;

    result = date1.before(date2);
    if (result == true)
      println("Date date1 comes before date2");
    else
      println("Date date2 comes before date1");

    result = date1.before(date3);
    if (result == true)
      println("Date date1 comes before date3");
    else
      println("Date date3 comes before date1");
  }
}

Output

Date date1 comes before date2
Date date3 comes before date1

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 three objects of the Date class and compare them using the before() method, and printed the appropriate message on the console screen.

Scala Date & Time Programs »





Comments and Discussions!

Load comments ↻





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