Java Program to Convert Date to TimeStamp Last Updated : 22 Oct, 2021 Comments Improve Suggest changes Like Article Like Report We can convert date to timestamp using the Timestamp class which is present in the SQL package. The constructor of the time-stamp class requires a long value.So data needs to be converted into a long value by using the getTime() method of the date class(which is present in the util package). Example: Input: Date is 19 October 2021 Output: 2021-10-19 18:11:24 Explanation: Date would be printed along with the current time to milliseconds.TimeStamp Class The TimeStamp class presents formatting and parsing methods to support JDBC escape syntax. It also combines the ability to hold the SQL TIMESTAMP fractional seconds value. How to use TimeStamp Class? Import the java.sql.Timestamp package.Import the java.util.Date packageCreate an object of the Date class.Convert it to long using getTime() method Syntax: public long getTime() Parameters: The function does not accept any parameter. Return Value: It returns the number of milliseconds since January 1, 1970, 00:00:00 GTM. Create an object of the Timestamp class and pass the value returned by the getTime() method.Finally, print this Timestamp object value. Example: Java // Java Program to convert date to time stamp import java.sql.Timestamp; import java.util.Date; public class GFG_Article { public static void main(String args[]) { // getting the system date Date date = new Date(); // getting the object of the Timestamp class Timestamp ts = new Timestamp(date.getTime()); // printing the timestamp of the current date System.out.println(ts); } } Output2021-10-19 20:18:08.813Formatting the TimeStamp Value:We can format the Timestamp value using SimpleDateFormat class.Initially, by using the Timestamp class, the time is getting displayed in a standard format, but we can format it to our own choice using SimpleDateFormat class. Example: Java // Java program to convert date to time-stamp using // SimpleDataFormat class and TimeStamp class import java.sql.Timestamp; import java.util.Date; import java.text.SimpleDateFormat; public class GFG { public static void main(String args[]) { // getting the system date Date date = new Date(); // getting the timestamp object Timestamp ts = new Timestamp(date.getTime()); // using SimpleDateFormat class,we can format the // time-stamp according to ourselves // getting the timestamp upto sec SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(formatter.format(ts)); // getting the timestamp to seconds SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm"); // printing the timestamp System.out.println(formatter1.format(ts)); } } Output2021-10-19 20:21:34 2021-10-19 20:21 Comment More infoAdvertise with us Next Article Java Program to Convert TimeStamp to Date L lavishgarg26 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-Date-Time +1 More Practice Tags : Java Similar Reads Java Program to Convert TimeStamp to Date Date Time class is used to display date and time and manipulate date and time in java and in addition to this it is also used for formatting date and time class in java across time zone associated data. So in order to import this class from a package called java.utils Timestamp class can be converte 3 min read Java Program to Convert Date to String The date type object can be converted to a string in Java using a large variety of in-built classes available in Java. Given a date, we need to convert the date to a given format of the string. Examples: Input: date = â2020-11-13âOutput: 2020-11-13 Input: date = â2020-12-14âOutput: 2020-12-14 Method 3 min read How to Convert a String to a Timestamp in Java? In this article, we will learn how to convert a String to a timestamp in Java, for this java provides a built-in package that is java.sql.Timestamp. By using this package, we can be able to convert the String value into the required timestamp format. But one thing we need to remember that is we need 2 min read Java Program to Get Today's Date Java is the most powerful programming language, by which we can perform many tasks and Java is an industry preferable language. So it is filled with a huge amount of features. Here we are going to discuss one of the best features of Java, which is how to get today's or current date using Java. Metho 2 min read Java Program to Display Current Date and Time Java is a most powerful programming language, by which we can do many things and Java is an industry preferable language. So it has a huge field of features. Here we discuss one of the best features of Java, that is how to represent the current date and time using Java. There are many ways to do thi 3 min read How to convert Date to String in Java Given a date, the task is to write a Java program to convert the given date into a string. Examples: Input: date = "2020-07-27" Output: 2020-07-27 Input: date = "2018-02-17" Output: 2018-02-17 Method 1: Using DateFormat.format() method Approach: Get the date to be converted.Create an instance of Sim 3 min read Like