Home » Scala language

How to check current Date and time in Scala?

Checking current date and time: In general-purpose programming date and time variables play an important role like when a message is sent, etc. And Scala also provides you ways to get current date and time. In this tutorial, we will see methods to check current date and time in Scala program.
Submitted by Shivang Yadav, on July 16, 2019

Scala is a general-purpose programming language, which is majorly used for data manipulation. It has libraries and support for almost all different utilities that are important. One of the important things that are required while programming is real-time date and time status and Scala has a solution for that too. This tutorial on check current date and time in Scala walks you through the method to create Scala variables that hold date and time stamps.

Getting current Date and Time in Scala using calendar class

Getting current date and time in Scala is very simple and you can do this using the classical java 'calendar' class.

This class can be imported using the statement: "java.util.Calendar". The calendar class in java is an abstract class that is used to get the current date and time. The same class is used in Scala too, we can get the current date and time using it. There is a field in this class that stores the current date and time.

Program to get time using getInstance() method

import java.util.Calendar;
object MyClass {
      def main(args: Array[String]) {
          var dT = Calendar.getInstance()
          var currentMinute = dT.get(Calendar.MINUTE)
          var currentHour = dT.get(Calendar.HOUR_OF_DAY)
          if(currentHour > 12){
              currentHour %= 12
              println("Current time is "+currentHour+":"+currentMinute+" PM")
          }
          else{
            println("Current time is "+currentHour+":"+currentMinute+" AM")  
          }
          
      }
   }

Output

Current time is 7:50 PM

Code logic

In this program, we have used the getInstance() method of the Calendar class. This method is used to find the date and time in java, the same method is used in Scala also, We have passed the variables calendar.MINUTE and calendar.HOUR_OF_DAY, and stored the result of the methods in variables currentMinute and currentHour respectively. The values returned from the functions are stored in the variables, The method gets (Calendar.HOUR_OF_DAY) returns the current hour in 24-hour format. To convert this into 12-hour format we will use a conditional statement that checks for AM or PM.

Program to get full date and time in Scala using Calendar Class

import java.util.Calendar;

object MyClass {
    def main(args: Array[String]) {
        var dT = Calendar.getInstance()
        var currentHour = dT.getTime()
        println("Current data and time is "+currentHour)    
    }
}

Output

Current data and time is Tue Jul 16 19:54:59 UTC 2019

Code logic

This code uses the getTime() method that returns the current date and time in the format day MM DD time UTC YYYY. This is also an inbuilt method of the class calendar.

You can also get the exact date and time of your current time based on UTC. Some methods help you get year, day, minute, the second also. You can get all this using the get() method of the Calendar class. Passing different parameters can get you different results. These are,

  • get(object.YEAR)
  • get(object.DATE)
  • get(object.MINUTE)
  • get(object.SECOND)

Program to find year, date, minute and second using get() method in Scala

import java.util.Calendar;

object MyClass {
    def main(args: Array[String]) {
        var dT = Calendar.getInstance();
        
        println("Current Calendar's Year: " + dT.get(Calendar.YEAR)); 
        println("Current Calendar's Day: " + dT.get(Calendar.DATE)); 
        println("Current MINUTE: " + dT.get(Calendar.MINUTE)); 
        println("Current SECOND: " + dT.get(Calendar.SECOND));  
    }
}

Output

Current Calendar's Year: 2019
Current Calendar's Day: 16
Current MINUTE: 0
Current SECOND: 5

Get Date using java.time.localDate.Now

This java method is available in Scala, you can get the current date using this method.

Program to get DATE in Scala

import java.util.Calendar;

object MyClass {
    def main(args: Array[String]) {
        println(java.time.LocalDate.now)
    }
}

Output

2019-07-16

Get Date using SimpleDateFormat class

This java class is also used in Scala to get current date. This class is used in Scala to get a date. This method is used with the scala calendar class to format the date into a specific form of our choice.

This class can be used by using the import statement: java.text.SimpleDateFormat. This imports these Java classes in Scala

Program to get date using simpleDateFormat in Scala

import java.util.Calendar;
import java.text.SimpleDateFormat;

object MyClass {
      def main(args: Array[String]) {
        val form = new SimpleDateFormat("dd / MM / yy"); 
        val c = Calendar.getInstance(); 
        
        println("Present Date : " + c.getTime()); 

        val formattedDate = form.format(c.getTime()); 
        println("Date formatted : "+formattedDate); 
      }
   }

Output

Present Date : Tue Jul 16 20:07:22 UTC 2019
Date formatted : 16 / 07 / 19

Get Time using SimpleDateFormat class

You can get the current time using the hour formatting over the calendar method variable.

  • "HH" for getting hour in 24 hour format
  • "hh" for getting hour in 12 hour format
  • "MM/mm" for getting Minutes
  • "ss" for getting seconds
  • "a" for getting am or pm

Program to get current time using SimpleDateFormat class

import java.util.Calendar;
import java.text.SimpleDateFormat;

object MyClass {
    def main(args: Array[String]) {
        val c = Calendar.getInstance(); 
        val hr24 = new SimpleDateFormat("HH"); 
        val formhr24 = hr24.format(c.getTime());
        val hr12 = new SimpleDateFormat("hh"); 
        val formhr12 = hr12.format(c.getTime()); 
        val min = new SimpleDateFormat("mm"); 
        val formmin = min.format(c.getTime()); 
        val sec = new SimpleDateFormat("ss"); 
        val formsec = sec.format(c.getTime()); 
        val a_p = new SimpleDateFormat("a"); 
        val forma_p = a_p.format(c.getTime()); 
        
        println("Time in 24 hour format "+formhr24+" : "+formmin+" : "+formsec)
        println("Time in 24 hour format "+formhr12+" : "+formmin+" : "+formsec+" "+forma_p)
    }
}

Output

Time in 24 hour format 20 : 12 : 18
Time in 24 hour format 08 : 12 : 18 PM

The Java functions calendar() and SimpleDateFormat() are valid in Scala also. As in the above programs - you can see that the methods of these classes that are imported from Java are so powerful that they can manipulate all the date endtime functionalities that are inbuilt in the compiler. You can check ok dates in any format. Using this you can also see which calendar the compiler is using and the time that is being used by the compiler. Generally, the compiler uses UTC and Georgian calendar.



Comments and Discussions!

Load comments ↻





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