The java.sql.Timestamp
is a wrapper around the java.util.Date
to map the SQL Timestamp values.
You can use Timestamp.valueOf(LocalDateTime.parse(“01022022 12:23:23”, DateTimeFormatter.ofPattern(“ddMMyyyy HH:mm:ss”))) function to convert Java String to sql.Timestamp object.
In this tutorial, you will learn different ways to convert a Java String to sq.Timestamp in detail.
Using LocalDateTime
The Java 8 LocalDateTIme can be used along with DateTimeFormatter to parse custom date formatted string to date-time values.
Check this tutorial for a detailed understanding of parsing a string to LocalDateTime, How to parse/format dates with Java 8 LocalDateTime
To convert a Java string to SQL.Timstamp
value using LocalDateTime,
- Define a DateTimeFormatter with a pattern(
"ddMMyyyy HH:mm:ss"
) matching the date string(01022022 12:23:23
). - Use the
LocalDateTime.parse(string, formatter)
function to convert the date-time string to the LocalDateTime value. - Invoke the
Timestamp.valueOf(localDateTime)
method to convert the LocalDateTime tosql.Timestamp
value.
Use this method to convert ISO 8601 formatted string or custom formatted string values to sql.Timestamp value.
Code
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("ddMMyyyy HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.parse("01022022 12:23:23", dateTimeFormatter);
Timestamp timeStamp = Timestamp.valueOf(localDateTime);
System.out.println(timeStamp); //2022-02-01 12:23:23.0
In the above example, the custom DateTimeFormatter accepts a date-time string in a specific format and converts it to a LocalDateTime value. Then using the Timestamp.valueOf()
method, the LocalDateTime value is converted to the Timestamp value.
Using TimeStamp formatted String
The Java sql.Timestamp
represent date-time in specific pattern(yyyy-mm-dd hh:mm:ss / yyyy-mm-dd hh:mm:ss.fff..
).
An sql.Timestamp
formatted string can be converted to a Timestamp object using the Timestamp.valueOf(timeStampStr)
method.
Use this method to convert simple timestamp formatted("yyyy-mm-dd hh:mm:ss.fff.."
) string.
Code
import java.sql.Timestamp;
//main
String timeStampStr = "2011-10-02 18:48:05.123456";
Timestamp timeStamp = Timestamp.valueOf(timeStampStr);
System.out.println(timeStamp); //2011-10-02 18:48:05.123456
String timeStampStr1 = "2011-10-02 18:48:05";
Timestamp timeStamp1 = Timestamp.valueOf(timeStampStr1);
System.out.println(timeStamp1); //2011-10-02 18:48:05.0
Using Date and Timestamp constructor
The Java Date class represents an instant in time.
To convert a Date string to SQL.Timstamp
value,
- Parse a date string to Date using the SimpleDateFormat.
- Create a Timestamp value using Timestamp constructor(
new Timestamp()
). - Pass the date milliseconds value extracted using
date.getTime()
to the constructor to get the Timestamp object.
Use this method for applications that still use java.util.date
for handling date values.
Code
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//main throws ParseException
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = dateFormat.parse("12/10/1990");
Timestamp timeStamp = new Timestamp(date.getTime());
System.out.println(timeStamp); //1990-10-12 00:00:00.0
Using Instant
The Java 8 Instant is used to get the point-in-time value.
To get the Timestamp value using Instant,
- Convert the millisecond’s values to Instant values using
Instant.ofEpochMilli(epochMilli).
- Pass Instant value to
Timestamp.from(instant)
method and it returns a Timestamp value.
Use this method when applications use date-time values in milliseconds.
Code
import java.sql.Timestamp;
import java.time.Instant;
//main
String milliSeconds = "1677697076000";
long epochMilli = Long.parseLong(milliSeconds);
Timestamp timestamp = Timestamp.from(Instant.ofEpochMilli(epochMilli));
System.out.println(timestamp); //2023-03-01 18:57:56.0