×

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on how to change a Long value in Java into a Date value. Let's now get into the specifics of the article.

What is Long in Java?

A simple data type, the long keyword is used in the Java programming language. In Java, the long keyword is used to declare large integer variables. A variable declared with the long keyword has an 8-byte default size and the value 0L by default. The long keyword may also be combined with methods. Let's now use a Java programming example application to understand the long data type.

Long is a primitive data type in Java. To declare variables, use this. It can also be applied to methods. A 64-bit two's complement integer can fit within it.

Remember:

  • A long's minimum value is -263 and its greatest value is 263-1.
  • With the introduction of Java 8, the long can now be represented as an unsigned 64-bit long with a minimum value of 0 and a maximum value of 264-1.
  • The default setting is 0L.
  • It is an 8-byte default size.
  • When a larger range of integer values is required, it is employed.

Program to determine the usage of long

Filename: Example1.java

//Using the Java programming language, this program 
//demonstrates how the "long" data type functions.
public class Example1 {  
    public static void main(String args[]) {  
//two variables are declared using the long data type.
        long S = 150000L ;  
        long V = -1500000L ;  
        // printing the values of long data types S and V.  
        System.out.println("The value of S is: " + S) ;  
        System.out.println("The value of V is: " + V) ;  
    }  
}  

Output

Converting Long to Date in Java

What is the Date in Java?

The Date is a class used to represent a particular moment in time. The Java Date class allows for millisecond-level precision when representing time. The "java.util" package contains the Date class, which provides a serializable, comparable, and cloneable interface. When dealing with date and time in Java, the Date class can be utilized using constructors and methods. The Date class uses a number of different constructors. Let's look at a few of the Date class's constructors.

How to convert Long to Date in Java?

We will start by providing an input of milliseconds, which is regarded as belonging to the "long" data type. Therefore, it is necessary to write a program that translates milliseconds that are initially in "long datatype" format into "date" format. The format used to display the date and time is often "dd Mmm yyyy HH: mm: ss: SSS Z," where "dd" stands for the date, "Mmm" for the month name's first three characters, and "yyyy" for the year. "HH" stands for the hour, "mm" for the minute, "ss" for the second, "SSS" for the first three digits of the millisecond, and "Z" stands for the time zone, These all relate to the date.

The milliseconds are determined using the date "1 January 1971," as it is when the idea of "date class" was developed and put into use. A constructor that assists the system in converting milliseconds into dates is offered by the "date" class. We can convert dates into the necessary format, i.e., " dd Mmm yyyy HH: mm: ss: SSS Z ", with the aid of the class " SimpleDateFormat ". Internally, the "SimpleDateFormat" class has a small number of constructors that handle formatting depending on the quantity and kind of parameters given. The class's constructors will be covered in detail below.

Constructors of SimpleDateFormat class:

Three distinct constructors are divided into categories based on the number of parameters and type of parameters passed. As follows:

  • SimpleDateFormat( String pattern_arg )
  • SimpleDateFormat( String pattern_arg, Locale locale_arg )
  • SimpleDateFormat( String pattern_arg, DateFormatSymbols formatSymbols)

SimpleDateFormat( String pattern_arg ):

The pattern "pattern arg" often creates a standard and Simple Date format that activates the default date format symbols for the  default FORMAT locale.

SimpleDateFormat( String pattern_arg, Locale locale_arg ):

While enabling the default date format symbols for FORMAT Locale " locale arg ", it often builds a Simple Date format in the pattern " pattern arg ".

SimpleDateFormat(String pattern_arg, DateFormatSymbols formatSymbols):

Typically, it creates a Simple Date format with any date format symbol enabled using the pattern "pattern_arg".

These three constructors provide a simplified example of the date format. Let's implement a handful of these constructors and create a program using the formatting class.

Program 1: demonstrates the conversion of milliseconds to Date:

Filename: Example4.java

//Java program that changes 
//milliseconds into date format


//to meet the criteria, 
//import the necessary packages 
  
import java.util.Date ;  
import java.text.DateFormat ;  
import java.text.SimpleDateFormat ;  
  
// main class  
  
public class Example4  
{  
    public static void main(String args[])  
    {  
//assigning a variable that
//stores the millisecond’s value 
        long x = 3000000000000L ;  
   
//starting the date format where 
//the conversion needs to be done 
  
        DateFormat obj = new SimpleDateFormat("dd MMM yyyy HH:mm:ss:SSS Z") ;  
   
        // Creation of date from milliseconds  
        // using the " Date " constructor  
        Date sol = new Date(x) ;  
   
        // Formatting Date according to the  
        // given format  
        System.out.println(obj.format(sol)) ;  
    }  
}  

Output

Converting Long to Date in Java

Program 2: demonstrates the conversion of milliseconds to Date:

Filename: Example5.java

//Java program that changes 
//milliseconds into date format


//to meet the criteria, 
//import the necessary packages 


  
import java.util.Date ;  
import java.text.DateFormat ;  
import java.text.SimpleDateFormat ;  
  
// main class  
  
public class Example5  
{  
    public static void main(String args[])  
    {  
          //assigning a variable that
          //stores the millisecond’s value
        long x = 409063289456L;  
    //The date format that must be used
    //for the conversion is being initiated. 
  
    DateFormat obj2 = new SimpleDateFormat("dd MMM yyyy HH:mm:ss:SSS Z") ;  
   
        // Creation of date from milliseconds  
        // using the " Date " constructor  
        Date sol2 = new Date(x) ;  
   
        // Formatting Date according to the  
        // given format  
        System.out.println(obj2.format(sol2)) ;  
    }  
}  

Output

Converting Long to Date in Java

Related Topics

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

Java Polymorphism

The process of representing one form in multiple forms is known as Polymorphism. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means...

5 minutes read.

Java Integer lowestOneBit()

The lowestOneBit () method of Java Integer class returns an int value with at most a single one-bit, in the position of the lowest-order one-bit in the specified int value.  Syntax public...

2 minutes read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

Thread States in Java

An Item's Life Cycle (Thread States) A thread can be in any of the states mentioned above at any given time in Java. They are as follows: New Active I'm blocked or waiting Temporary...

3 minutes read.

Insertion Sort in Java

Insertion Sort in Java Insertion sort in Java is a simple sorting algorithm that works in the same way as we hold cards in hand. Insertion sort does the sorting element-by-element,...

3 minutes read.

Difference between Abstract Class and Interface

There is a similarity between abstract class and interface is that we cannot create objects for both of them. But irrespective of this, there are some differences between them, let’s...

2 minutes read.

Transaction Management in java

Definition: A database application is an application that is running against a relational database and executes one or more transactions. A transaction is an executing program that contains some database operations,...

4 minutes read.

The final Keyword in Java

The final keyword is employed in several instances. Firstly, the non-access modifier final only applies to variables, methods, and classes. The final can be used in the following situations. Final Variables When...

6 minutes read.

How to convert String to String array in Java

A String in Java is a thing that indicates a collection of letters. We must include the String class from java.lang package if we want to be using strings. An...

5 minutes read.

Check the presence of Substring in a String in java

In java, the string can be treated as class and datatype. The string contains words and numbers but should be in double-quotes. Example: ” Omsairam” Substring The part of the string is called...

2 minutes read.

Difference between next() and nextline() in Java

One of the simplest methods for receiving input of the basic data types, also including int, double, and strings, in Java, is to use the Scanner class, which is part...

3 minutes read.

String Manipulation in Java

String Manipulation in Java In Java, string manipulation is a common task performed by programmer. Java String class provides many built-in functions that are used to manipulate string. The manipulation of...

4 minutes read.

ArrayList Program in Java

ArrayList Program in Java: In Java, ArrayList is a class that belongs to java.util package. It is the dynamic list that grows or shrinks at run-time as per the requirements....

4 minutes read.

How to Create an Object in Java

How to Create an Object in Java An object can be defined as a run time entity that contains the blue printof the class. It means that all the member functions...

5 minutes read.

Difference Between Thread.start() and Thread.run()

In the Java programming language, the multi-threading concept consists of the start() and run() methods. Thread.start(): The thread's execution is initiated by invoking the start() method. The start() method operates two threads...

4 minutes read.

Find the Frequency of Each Element in the Array in Java

We may count the occurrence of each element in the array of items. Maintaining one array to store the counts of each array element is one strategy for solving this issue....

3 minutes read.

Java program to find frequency of characters in a string

In this article, you will understand the how to find the frequency of characters in strings by using Java programming language. Along with this, you will understand the hashing concept...

3 minutes read.

Magnanimous Number in java

Magnanimous Number When the left and right halves of a majestic number are combined, the result is invariably a prime number, which must have at least two digits. The number's left...

3 minutes read.

Java Enhanced For Loop (For-each Loop)

JAVA ENHANCED FOR LOOP The enhanced for loop is also called the for-each loop and has some advantages over the regular for loop. A for-each loop is designed to cycle through...

4 minutes read.