Home » Scala language

How to create a Java or Scala date from a long value?

In Scala programming, you can create a date from a long value. In the Scala programming tutorial, we will learn the method to create Java or Scala date from a long value.
Submitted by Shivang Yadav, on August 06, 2019

Scala programming language inherits its date formatting libraries from Java Mostly. So when it comes to date and time format, Java and Scala have the same set of libraries that work this means that the functionalities would be the same. let's explore this problem and see the possible solutions to the problem.

Problem: How to create Java or Scala date and time from a long value?

Solution:

In Scala programming, the Date class is responsible to format date and time. This class can be imported in Scala from Java libraries using the import statement: java.util.Date. Using the Date class we can convert the Long data type to date format.

To do this, we need to multiply the long with 1000L value. And then pass it into the Date Constructor.

Let's create a program for the solution of a specified problem:

Program to create a Scala date from long value

import java.util.Date; 

object myClass{    
    def main(args: Array[String]) {
        val i : Long = 1513714678
        val d = new Date(i * 1000L)
        println("The long value is "+i)
        println("The date conversion of this value is "+d)
    }
}

Output

The long value is 1513714678
The date conversion of this value is Tue Dec 19 20:17:58 GMT 2017

Program to create a Java date from long value

import java.util.Date; 

public class Main{
    public static void main(String[] args) {
        Long i = 1513714678L;
        Date d = new Date(i * 1000L);
        System.out.println("The long value is "+i);
        System.out.println("The date conversion of this value is "+d);
    }
} 

Output

The long value is 1513714678
The date conversion of this value is Tue Dec 19 20:17:58 GMT 2017


Comments and Discussions!

Load comments ↻





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