How to Convert Date to Timestamp in Java?


In Java, Date can be converted Timestamp by using Timestamp class. Timestamp class is present in sql package. Date only store date value whereas timestamp stores both date and time value. The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.

Let’s deep dive into this article, to know how it can be done by using Java programming language.

To show you some instances

Instance-1

  • Suppose the input date is written on 05/01/2023.

  • Then corresponding Timestamp is “2023-01-05 20:37:54.832”.

Instance-2

  • Suppose the input date is “16/04/2021”

  • Then corresponding Timestamp is “2022-04-04 04:12:35.0”.

Algorithm:

  • Step-1 − Declare the Date or take a default date from the system.

  • Step-2 − Convert it to timestamp using timestamp class.

  • Step-3 − Print the result.

Syntax

getTime() Method − It returns the number of milliseconds since January 1, 1970, 00:00:00 GTM which is represented by Date object.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Input Date From System

  • By Using Input Date as a String

Let’s see the program along with its output one by one.

Approach-1: By Using Input Date from System

In this approach, we take input date from system. Also, the constructor of Timestamp class receives long value as an argument. So we need to convert date into long value using getTime() method of java.util.Date class.

Example

import java.sql.Timestamp;
import java.util.Date;
public class Main {
   
   //main method
   public static void main(String args[]){
		
      //getting the system date
      Date date = new Date();

      //getting the object of the Timestamp class
      Timestamp tms = new Timestamp(date.getTime());

      // printing the result
      System.out.println(tms);
   }
}

Output

2023-01-05 20:37:54.832

Approach-2: By Using Input Date as a String

In this approach, we take date input as a String. Also constructor of Timestamp class receives long value as an argument. So we need to convert date into long value using getTime() method of java.util.Date class.

Example

import java.sql.Timestamp;
import java.util.Date;
public class Main {

   // Main method
   public static void main(String[] args){
	
      //taking a string  date    
      String date="2022/04/04 04:12:35";  

      //declaring timestamp
      Timestamp ts=null;  

      //Intialize date with the string date
      Date d=new Date(date); 

      // simple null check
      if(d!=null){ 
         
         // convert gettime from date and assign it to the timestamp
         ts=new java.sql.Timestamp(d.getTime()); 

         //printing the timestamp
         System.out.println(ts);
      }
   }
}

Output

2022-04-04 04:12:35.0

In this article, we explored different approaches to convert Date to Timestamp by using Java programming language.

Updated on: 04-May-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements