EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Scala Date Function

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Scala Tutorial » Scala Date Function

Scala Date Function

Definition of Scala Date Function

In scala we do not have any date-time library or function available, we can use java data time, calendar, Date library to deal with dates in Scala. By using these functions, we can convert our date to a string and vice versa, also we can format our date. By using this function, we can get the current date and time, location, etc on scala. This function can also return us the current timestamp and many more. We just need to import the packages in order to use date functions in scala. In the coming section, we will discuss more the date function and their usages in detail.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

As earlier explained we do not have any date function available in scala but still, we can use the java library to deal with this in scala. Let’s have a look at the syntax how to use them in programming see below;

1) import java.time.LocalDate
2) import java.time.format.DateTimeFormatter

var variable_name  = LocalDate.parse(date_as_string, DateTimeFormatter.ofPattern(Your_pattern))

As you can see in the above syntax we are using parse() function from date library which can be used to convert the date object into any specified format and pattern, But in order to use this, we have to import the java libraries in the program.

How does date function work in Scala?

As now we know that there is no date function available in Scala, we have to use the java library to deal with them in scala. Here in this section, we are trying to use the ‘DateTime’ library from java. This library provides various function which are easily used to deal with date in Scala. We will see how to import this library into our existing program and how we can use these functions to get the date object in scala. Also by using these functions, we can convert the date object into a string and vice versa. Also, we can convert them to any pattern and format as well. Let’s talk about some of the methods which are available in Date library see below;

1) import java.util.Date: Include this library into your program at the start because we do not have any specific way to deal with dates in Scala. We are using the java library for this. Inside this package, we have so many functions available which can be used to deal with dates. Let’s first discuss the constructor of this class see below;

  • Date(): This is used to get the current date and time. We can just use this with the ‘new’ keyword.

e.g. :

var d1 = new Date();

  • Date(String s): In this, we can pass the data as the string parameter and it will convert that string into a date object. See below for its usage in Scala;

e.g. :

var d1 = new Date("11/08/2020")

  • Date(int year, int month, int date): This takes three parameters as the input here. At first, we can pass the year, the second parameter accepts month and the last parameter accepts the date. After this, it will return us the date object in Scala. Let’s see its usage in Scala see below;

e.g. :

var d1 = new Date(2020, 09, 03)

  • Date(long milliseconds): In this, we can pass the milliseconds then according to this it will calculate the date and return us. Let’s see its usage in Scala see below;

e.g. :

var d1 = new Date(89808989)

  • Date(int year, int month, int date, int hrs, int min): This constructor object takes five parameters and the basis of it will create the date object. Inside this, we can pass year, month, date, hours, and minutes respectively as the input param. Let’s see its usage in scala see below;

e.g. :

var d1 = new Date(2020, 08, 19, 13, 50)

  • Date(int year, int month, int date, int hrs, int min, int sec): This constructor object takes five parameters and the basis of it will create the date object. Inside this, we can pass year, month, date, hours, minutes, and seconds respectively as the input param. Let’s see its usage in scala see below;

e.g. :

var d1 = new Date(2020, 08, 19, 13, 50, 00)

Popular Course in this category
Scala Programming Training (3 Courses,1Project)3 Online Courses | 9+ Hours | Verifiable Certificate of Completion | Lifetime Validity | 1 Project
4.5 (5,543 ratings)
Course Price

View Course

Related Courses
Programming Languages Training (41 Courses, 13+ Projects, 4 Quizzes)All in One Software Development Bundle (600+ Courses, 50+ projects)

Examples

In this example we are creating the date object it will give us today date.

Example #1

Code:

import java.util.Date
object Main extends App{
// Your code here!
println("Demo for date function in scala !!!")
var d1 = new Date()
println(d1);
}

Output:

Scala Date Function 1

Example #2

In this example, we are creating date by using year month, and date passing inside the constructor in scala.

Code:

import java.util.Date
object Main extends App{
// Your code here!
println("Demo for date function in scala !!!")
var d1 = new Date(2020, 08, 19)
var d2 = new Date(2019, 07, 18)
var d3 = new Date(2018, 06, 17)
var d4 = new Date(2017, 05, 16)
var d5 = new Date(2016, 04, 15)
println("result for date one is  ::: ")
println(d1);
println("result for date two is  ::: ")
println(d2);
println("result for date three is  ::: ")
println(d3);
println("result for date four is  ::: ")
println(d4);
println("result for date five is  ::: ")
println(d5);
}

Output:

Scala Date Function 2

Example #3

In this example, we are creating date by using year month, date, hour, and minute passing inside the constructor in Scala.

Code:

import java.util.Date
object Main extends App{
// Your code here!
println("Demo for date function in scala !!!")
var d1 = new Date(2020, 08, 19, 01, 20)
var d2 = new Date(2019, 07, 18, 10, 30)
var d3 = new Date(2018, 06, 17, 13, 30)
var d4 = new Date(2017, 05, 16, 14, 20)
var d5 = new Date(2016, 04, 15, 20, 22)
println("result for date one is  ::: ")
println(d1);
println("result for date two is  ::: ")
println(d2);
println("result for date three is  ::: ")
println(d3);
println("result for date four is  ::: ")
println(d4);
println("result for date five is  ::: ")
println(d5);
}

Output:

Scala Date Function 3

Conclusion

So to use date in scala we can use java library by importing packages. After this we can use its function to deal with date and time in scala, we can parse, format, and create the date from string object as well.

Recommended Articles

This is a guide to Scala Date Function. Here we also discuss the definition and How does Date function work in Scala? along with different examples. You may also have a look at the following articles to learn more –

  1. Scala Vector
  2. Scala Seq
  3. Scala Byte
  4. Scala ListBuffer

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Scala Tutorial
  • Advanced
    • Constructors in Scala
    • Scala Class Constructor
    • Scala Abstract Class
    • Scala String
    • Scala Collections
    • Scala High Order Functions
    • Scala Date Function
    • Scala Stream
    • Scala Class
    • Scala Function
    • Scala Closure
    • Scala Range
    • Scala Either
    • Scala Random
    • Scala Tuples
    • Scala Partial Function
    • Scala Split
    • Scala Finds
    • Scala List
    • Scala Trait
    • Scala Try Catch
    • Scala Regex
    • Scala Vector
    • Scala Seq
    • Scala Byte
    • Scala ListBuffer
    • Scala ArrayBuffer
    • Scala flatMap
    • Scala HashMap
    • Scala Map Function
    • Scala SortBy
    • Scala Some
    • Scala Generic
    • Scala getOrElse
    • Scala REPL
    • Scala Logging
    • Scala Lambda
    • Scala Queue
    • Scala Set
    • Scala Singleton
    • Scala groupBy
    • Scala reduce
    • Scala filter
    • Scala Option
    • Scala Native
    • Scala DataFrame
    • Scala Anonymous Function
    • Scala Read File
    • Scala Write to File
    • Scala Pattern Matching
    • Scala ClassTag
    • Scala Synchronized
    • Scala zipwithindex
    • Scala Interview Questions
  • Basics
    • What is Scala?
    • Uses of Scala
    • Scala Versions
    • Scala Data Types
    • Scala Companion Object
    • Scala Operators
    • Scala: The Functional Approach
    • Scala Object
    • Scala Implicit
    • Scala IDE
  • Control Flow
    • Scala if else
    • Break in Scala
    • Scala for Loop
    • Scala foreach
    • Scala While Loops

Related Courses

Scala Programming Training Course

Programming Language Course

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Scala Programming Training Course Learn More