×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

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

By IncludeHelp Last updated : November 15, 2024

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.

Create Java (or, Scala) Date and Time from Long Value

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]): Unit = {
        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.