×

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

How Many Ways to Create an Object in Java?

As you know, a class is a blueprint for an object, and you can create objects from it. There are several ways to create objects of classes in Java. This...

6 minutes read.

Access Modifier in Java

The access modifiers in java are used to change the accessibility and scope of a method, constructor, class, and fields. If you are aware of C++ language, when we declare any member...

2 minutes read.

Java String toUpperCase() methods

Java String toUpperCase() method is used to convert all the characters of the String into upper case. Syntax public String toUpperCase() public String toUpperCase(Locale locale) Returns It returns upper case String. Java String toUpperCase() Example 1: public...

1 minute read.

Java Lock

A lock is indeed a threaded synchronization technique similar to Java's synchronized blocks, however, locking can be more complex. It's not like we can completely get rid of the synchronized...

5 minutes read.

How to Read CSV Files in Java?

Comma-Separated Values is the acronym for this format. It is a straightforward file format storing textual tabular data in a database or spreadsheet. The CSV files can be imported into...

3 minutes read.

Static() Function in Java

The static keyword in Java is suitable for variables, constants, and functions. The static keyword is mainly used to control storage so that it may be appropriately used. We shall...

3 minutes read.

Interchange Diagonal elements in Java

In this article, you will be very well acknowledged about what is a matrix with an example. You will also be acknowledged about how to interchange the diagonal elements in...

4 minutes read.

Shopping Bill in Java

Java Shopping Bill Here in this program, we are about to create a JAVA class called Products which will have some properties or attributes like prod_name (Product Name ), qty (quantity),...

4 minutes read.

Arithmetic exception in Java

Exception Handling is one of the most potent ways of handling runtime faults and preserving the application's normal flow. In Java, an exception is an out-of-the-ordinary state, and exceptions are...

3 minutes read.

Package naming convention in Java

It is conceivable that many programmers will use the same name for various types, given that Java programmers from all over the world create classes and interfaces. For illustration, suppose...

4 minutes read.

How many ways to create object in Java?

In this article, you will be acknowledged about the different ways to create an object in java. So far you construct an object from a class, as is common knowledge,...

6 minutes read.

Java Substring

What is a substring in Java? Here by the name  " substring " itself, we can easily come to know it is a part of a string or a subset of...

4 minutes read.

Int vs Integer in Java

In Java, numerical data can be stored using int or Integer. int and Integer both have different definitions but similar usage in Java. What is int in Java? int is a primitive...

4 minutes read.

Bifunction in java 8

A functional interface in Java is called BiFunction. It first appeared in Java 8. It can serve as the assigning target for a method reference or lambda expression. The java.util.function...

4 minutes read.

Difference between String Tokenizer and split Method in Java

Introduction Today, let us understand the difference between String Tokenizer and Split Method. First, let us learn about the String Tokenizer and Split method individually and then know about their differences String...

7 minutes read.

Java Programming certification

The most common technology utilized in the creation of applications is Java. People and businesses like it because it turns original ideas into useful software solutions. A Java programming certification can...

6 minutes read.

Virtual Function in Java

In the Operated Oriented Programming language, a virtual function or virtual method is a collection of functions that overrides the functionality of a function in an inheriting class with the same...

4 minutes read.

Shift right zero Fill Operator in Java and Operator Shifting

Left shift operator ( << ) : The left shift operator is an operator which performs its action at the bits level of a binary Operator. That means when you perform...

4 minutes read.

Different Ways to Print Exception Message in Java

In this section, we will learn how to use various Java Throwable class methods to print exception messages. The Throwable class has the three methods listed below for printing the...

4 minutes read.

Two Decimal Places Java

When a double data type is used in Java before a variable, this indicates 15 digits after decimal point. However, there are situations when we only require 2 decimal places...

4 minutes read.