Converting Long to Date in Java
In this article, we are about to learn what are Long and Date in Java and their implementation in the Java Programming language. We are also going to discuss in-depth how to convert a Long value into a Date value in the Java programming language. Now let us go into the details of the article.
What is Long in Java?
The long keyword in the Java programming language is a primitive data type. The purpose of long Keyword in Java is to declare large variables of integer type. The default size of a variable declared using the long keyword is 8 bytes, and its default value is 0L. The long keyword can also be used in combination with methods. Now let us understand the long data type in the Java programming language using an example program.
Program to determine the usage of long:
The output of the program:
The value of A is: 150000 The value of B is: -1500000
What is the Date in Java:
In the Java programming language, Date is a class which is used to represent a specific instant of time. Time is represented with millisecond precision while using the Date class in Java. Date class belongs to the "java.util" package, and it implements a serializable, comparable and cloneable interface. The Date class can be used with constructors and methods to deal with date and time in Java. There are various constructors that are used in the Date class. Let us see a few constructors that are used in the Date class.
Constructors provided with Date class:
A program showing constructors of Date class:
The output of the program:
The current date is: Sat Aug 13 15:28:16 IST 2022 The represented date is: Wed Jan 28 02:50:23 IST 1970
There are some important methods that are used with the Date class. Now let us see a few of the important methods that are used with the Date class.
Methods that are provided with the Date class:
A program showing Methods of Date class:
Output for the above Program:
date3 comes after date2: true date3 comes before date2: false 1 The total number of milliseconds from Jan 1 1970 to date date1 is 60999330600000 Before setting the time date2 is: Wed Aug 17 22:33:50 IST 2022 After setting the time date2 is: Sat Jun 26 03:20:33 IST 1976
How to convert Long to Date in Java?
Initially, we will give an input of the milliseconds, which is considered to be in the data type " long ". So, a program must be written in such a way so that it converts the milliseconds which are initially in the " long datatype " into the " date " format. The date and the corresponding time are generally displayed in the " dd Mmm yyyy HH: mm: ss: SSS Z " format where " dd " represents the date, " Mmm " denotes the first three letters of the month name, " yyyy " denotes the year. These all correspond to the date and " HH " denotes the hour, " mm " denotes the minutes, " ss " denotes the seconds, " SSS " denotes the first three digits of the milliseconds, and " Z " denotes the time zone.
The milliseconds are calculated from the date " 1 January 1971 " as the concept of " date class " was discovered and implemented on that day. The " date " class provides a constructor which helps the system to convert the milliseconds into the date. The class " SimpleDateFormat " helps us to convert the date into the required format, i.e., " dd Mmm yyyy HH: mm: ss: SSS Z ". Internally, the class " SimpleDateFormat " possess few constructors which deal with the formatting based on the number of parameters passed and the type of parameters passed. Let us discuss everything about the class and its constructors below.
Constructors of SimpleDateFormat class:
There are three different constructors, which are classified depending on the number of parameters and the type of parameters passed. They are:
SimpleDateFormat( String pattern_arg ):
It generally constructs a normal and Simple Date format in the pattern " pattern_arg", which enables the default date format symbols for the default FORMAT locale.
SimpleDateFormat( String pattern_arg, Locale locale_arg ):
It generally constructs a Simple Date format in the pattern " pattern_arg" but enables the default date format symbols for FORMAT Locale " locale_arg ".
SimpleDateFormat( String pattern_arg, DateFormatSymbols formatSymbols):
It generally constructs a Simple Date format in the pattern " pattern_arg" and enables any type of date format symbols.
These are the three constructors that demonstrate the date format in a simpler version. Let us implement a few of these constructors and use the formatting class to build a program.
Program 1 that demonstrates the conversion of milliseconds to Date:
Output 1 for the above Program:
24 Jan 2065 10:50:00:000 +0530
Program 2 that demonstrates the conversion of milliseconds to Date:
Output 2 for the above Program:
18 Dec 1982 18:11:29:456 +0530+0530
Next TopicJava 17 new features