Open In App

How to Convert a String to a Timestamp in Java?

Last Updated : 05 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 to take the required timestamp value as a String value then only we can solve this problem. Now we will learn steps for solving this problem using Java. programming.

Steps to convert a String to a timestamp in Java

  • Step 1: Create one Java class.
  • Step 2: After this in the main method, take one string value that represents the date and time.
  • Step 3: After that define the required Date Format.
  • Step 4: Then convert the timestamp into an already defined Date Format.
  • Step 5: The final step is to display the result.

Java Program to Convert a String to a Timestamp

In this Java code, we have converted the String value into the required timestamp format. You can see the below java code for a better understanding of the concept.

Java




import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
  
public class StringToTimestampExampleOne {
  
    public static void main(String[] args) {
        // The input string representing a date and time
        String dateString = "2024-01-26 12:30:45";
  
        // Define the date format of the input string
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  
        try {
            // Parse the input string to a java.util.Date object
            java.util.Date parsedDate = dateFormat.parse(dateString);
  
            // Convert java.util.Date to java.sql.Timestamp
            Timestamp timestamp = new Timestamp(parsedDate.getTime());
  
            // Print the result
            System.out.println("Input String: " + dateString +" And It data type is"+ dateString.getClass());
            System.out.println("Converted Timestamp: "+ timestamp +" And It data type is "+ timestamp.getClass());
  
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}


Output in console:

Timestamp Output

Explanation of the above Program

In above Java code,

  • First, we have taken timestamp value as String value.
  • After that we have defined the required date format.
  • In this example, we have used yyyy-MM-dd HH:mm:ss – It represent the year-month-day hours:minutes:seconds.
  • Now we have defined the Timestamp Class for converting String to timestamp by using Date class and Its object.
  • After that print the result and print Its class names also for better understanding.
  • The result gives one clarity means before converting, the String is belonging java.lang.String and after converting the String value into Timestamp, its class type is java.sql.Timestamp.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads