ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Scala program to compare two dates

Here, we are going to learn how to compare two dates in Scala programming language?
Submitted by Nidhi, on May 30, 2021

Problem Solution:

Here, we will create three objects of the Date class and compare date objects using the "==" operator and print an appropriate message on the console screen.

Program/Source Code:

The source code to compare the two dates is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to compare two dates

import java.util.Date;

object Sample {
  def main(args: Array[String]) {
    var d1 = new Date(2020, 6, 20)
    var d2 = new Date(2019, 5, 20)
    var d3 = new Date(2019, 5, 20)

    if (d1 == d2)
      println("matched");
    else
      println("Not matched");

    if (d2 == d3)
      println("matched");
    else
      println("Not matched");
  }
}

Output:

Not matched
matched

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 d1, d2, d3 of the Date class, and then compared objects using the "==" operator and printed the appropriate message 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.