Convert String to Timestamp in Java
-
Use
TimeStamp.valueOf()
to Convert a String to Timestamp in Java -
Use
Date.getTime()
to Convert a String to Timestamp in Java
In this article, we will introduce two methods to convert a string to a timestamp in Java. A timestamp is mainly used in databases to represent the exact time of some event. The Timestamp
class we will use in this tutorial is a part of the java.sql.Timestamp
package.
Use TimeStamp.valueOf()
to Convert a String to Timestamp in Java
We will use the TimeStamp
class’s own static function - valueOf()
. It takes a string as an argument and then converts it to a timestamp. One important thing to note here is to take care of the format in which the date and time are written in the string that we want to be converted into a timestamp. It is restricted to a fixed format, which is yyyy-mm-dd hh:mm:ss
.
We cannot change the format and then expect the right result, but instead, if we use an incorrect format, we will get an IllegalArgumentException
in the output. In the below example, we have used 2020-12-12 01:24:23
as the date and time in the string, which follows the correct format of yyyy-mm-dd hh:mm:ss
.
We can now pass dateTime
as the only argument of the valueOf(string)
method, and it will convert a string to a timestamp.
import java.sql.Timestamp;
public class StringToTimeStamp {
public static void main(String[] args) {
String dateTime = "2020-12-12 01:24:23";
Timestamp timestamp = Timestamp.valueOf(dateTime);
System.out.println(timestamp);
}
}
Output:
2020-12-12 01:24:23.0
We can get rid of the date and time formatting restrictions by using the same valueOf()
method, but instead of directly passing a string to the method, we will use the LocalDateTime
class. Because valueOf()
accepts a LocalDateTime
as an argument.
In the following code, dateTime
has a date and time which is then formatted using the DateTimeFormatter
class’s ofPatter()
method. We can use this formatter to parse and get a LocalDateTime
object using the LocalDateTime.from()
function.
Once we get a LocalDateTime
object, we can pass it to Timestamp.valueOf(localDateTime)
to convert the string to a timestamp.
import java.sql.Timestamp;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class StringToTimeStamp {
public static void main(String[] args) throws ParseException {
String dateTime = "01/10/2020 06:43:21";
DateTimeFormatter formatDateTime = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.from(formatDateTime.parse(dateTime));
Timestamp ts = Timestamp.valueOf(localDateTime);
System.out.println(ts);
}
}
Output:
2020-10-01 06:43:21.0
Use Date.getTime()
to Convert a String to Timestamp in Java
The second method to convert a string to a timestamp uses multiple classes and methods. Just like LocalDateTime
, we can use our date and time format in the string. We used the SimpleDateFormate()
class to format the string and then parse it to a Date
object.
We need the Date
object because it has the getTime()
object, which returns the date and time as long
. We can pass this long
value to the constructor of Timestamp
as we have done below.
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToTimeStamp {
public static void main(String[] args) throws ParseException {
String inDate = "01/10/2020 06:43:21";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date date = df.parse(inDate);
long time = date.getTime();
Timestamp ts = new Timestamp(time);
System.out.println(ts);
}
}
Output:
2020-01-10 06:43:21.0