Java Convert Date to Timestamp

To convert a java.util.Date object to a java.sql.Timestamp object in Java, you can use the following code:

// create a new java.util.Date object
Date date = new Date();

// convert the Date object to a Timestamp object
Timestamp timestamp = new Timestamp(date.getTime());

In this code, we first create a new java.util.Date object using the default constructor, which initializes the object to the current date and time. Then we create a new java.sql.Timestamp object by passing the result of date.getTime() as an argument. The getTime() method returns the number of milliseconds since January 1, 1970, 00:00:00 GMT, which is the standard epoch time used in Java.

You can also convert a java.util.Date object to a java.sql.Timestamp object using the valueOf method of the java.sql.Timestamp class, like this:

// create a new java.util.Date object
Date date = new Date();

// convert the Date object to a Timestamp object
Timestamp timestamp = Timestamp.valueOf(date.toString());

In this code, we first create a new java.util.Date object as before. Then we convert the Date object to a String using the toString() method, which returns a string representation of the date in a format like “Sat May 07 10:30:00 GMT 2023”. Finally, we pass this string to the valueOf method of the java.sql.Timestamp class, which parses the string and returns a Timestamp object.

Java Date to Timestamp Example:

Sure! Here’s an example of converting a java.util.Date object to a java.sql.Timestamp object in Java:

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

public class DateToTimestampExample {
    public static void main(String[] args) {
        // create a new java.util.Date object
        Date date = new Date();
        
        // convert the Date object to a Timestamp object
        Timestamp timestamp = new Timestamp(date.getTime());
        
        // print the original Date object and the converted Timestamp object
        System.out.println("Original Date object: " + date);
        System.out.println("Converted Timestamp object: " + timestamp);
    }
}

In this example, we first import the java.sql.Timestamp and java.util.Date classes. Then, we create a new java.util.Date object using the default constructor, which initializes the object to the current date and time.

Next, we convert the Date object to a Timestamp object using the Timestamp constructor that takes a long argument, which is the result of calling the getTime() method on the Date object. The getTime() method returns the number of milliseconds since January 1, 1970, 00:00:00 GMT.

Finally, we print both the original Date object and the converted Timestamp object to the console. The output should look something like this:

Original Date object: Fri May 07 16:23:14 EDT 2023
Converted Timestamp object: 2023-05-07 16:23:14.311

As you can see, the Timestamp object represents the same date and time as the Date object, but in a different format that is suitable for use with SQL databases.