How to Convert String Date to Timestamp in Java

In this tutorial, we will learn how to convert String Date to Timestamp in Java. This conversion is very important when we want to deal with timestamps. For example, one may be interested to know which date is smaller than the other, or we may want to count the number of days since a particular date and it is not possible with the string date. In this tutorial, we will use SimpleDateFormat, Timestamp, and Date class of Java.

Example for converting String a Date to Timestamp

The code is given below works perfectly for converting String Date to Timestamp. Firstly we will convert a simple string to Date using SimpleDateFormat.parse() method before that makes sure that timestamp is given in the correct format.

import java.text.SimpleDateFormat;
import java.sql.Timestamp;  
import java.util.Date;

public class StudyTonight 
{ 
	public static void main(String[] args) throws Exception 
	{ 
		//date in string format
		String stringDate = "2021-01-07 02:02:16.172";
		try
		{
			//creating date format
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
			//parsing string to date using parse() method
			Date parsedDate = dateFormat.parse(stringDate);
			//finally creating a timestamp
			Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
			System.out.println(timestamp.getClass());
		}
		catch(Exception e)
		{   
			System.out.println(e);
		}
	} 
} 


2021-01-07 02:02:16.172

If the string is in the incorrect format you will get an exception error: java.text.ParseException: Unparseable date:

Converting String date to Timestamp using valueOf() Method

This is another way of converting date string to timestamp using valueOf() method of java.sql.Timestamp class. This is a simple method and no need to do overhead coding.

Example 2

In this program, we do not need to think about a format for a date because valueOf() method will directly convert the string to the timestamp itself. Unlike the last code if the string is not in the correct format it will throw an exception java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff].

import java.text.SimpleDateFormat;
import java.sql.Timestamp;  
import java.util.Date;

public class StudyTonight 
{ 
	public static void main(String[] args) throws Exception 
	{ 
		//date in string format
		String stringDate = "2021-01-07 02:02:16.172";
		try
		{
			//converting string date to timestamp using valueOf() method
			java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf( stringDate );

			System.out.println(timestamp);
		}
		catch(Exception e)
		{   
			System.out.println(e);
		}
	} 
}


2021-01-07 02:02:16.172

Conclusion:

To convert the string date to timestamp we have two ways i.e. using TimeStamp as a constructor and another is using a valueOf() method of TimeStamp class. This is very useful when it comes to performing operations on actual timestamps rather than different formats of date.