Java Date - java.sql.Timestamp Class
Timestamp Class
java.sql.Timestamp was created to correspond to SQL’s TIMESTAMP type. It is used when manipulating date and time-related data with the Timestamp class.
Timestamp is a subclass that extends the java.util.Date class.
Understanding Timestamp
The Timestamp constructor specifies a time value in milliseconds.
If you use the currentTimeMillis() method of the System class, it returns the current time represented in milliseconds, so let’s use it.
Timestamp currentTimestamp = new Timestamp(System.currentTimeMillis());
System.out.println(currentTimestamp);
Result:
2022-12-12 08:03:15.759
Timestamp <-> String Conversion
Convert Timestamp to String
To convert Timestamp to String, use the SimpleDateFormat class, which is familiar for date and time formatting.
Timestamp currentTimestamp = new Timestamp(System.currentTimeMillis());
// Timestamp to String
String currentTimestampToString = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(currentTimestamp);
System.out.println(currentTimestampToString);
Result:
2022/12/12 08:03:15
The Timestamp has been converted to a String such as 2022/12/12 08:03:15.
Convert String to Timestamp
This time, let’s convert from String to Timestamp.
First, convert from String to the java.util.Date class. Then call getTime() on the converted Date object to get milliseconds, and pass that value to the Timestamp constructor.
String currentTimestampToString = "2022/12/12 08:03:15";
// String to Timestamp
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
dateFormat.setLenient(false);// Strictly check date and time
try {
Date stringToDate = dateFormat.parse(currentTimestampToString);
Timestamp stringToTimestamp = new Timestamp(stringToDate.getTime());
System.out.println(stringToTimestamp);
} catch (ParseException e) {
e.printStackTrace();
}
Result:
2022-12-12 08:03:15.0
The Timestamp created this way can be used when storing data in a database.
Full Conversion Code
The full code written above is as follows.
package com.devkuma.basic.datetime;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimestampConverter {
public static void main(String[] args) {
Timestamp currentTimestamp = new Timestamp(System.currentTimeMillis());
System.out.println(currentTimestamp);
// Timestamp to String
String currentTimestampToString = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(currentTimestamp);
System.out.println(currentTimestampToString);
// String to Timestamp
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
dateFormat.setLenient(false);// Strictly check date and time
try {
Date stringToDate = dateFormat.parse(currentTimestampToString);
Timestamp stringToTimestamp = new Timestamp(stringToDate.getTime());
System.out.println(stringToTimestamp);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
LocalDateTime Instead of Timestamp
In Java 8, Instant, LocalDateTime, and ZonedDateTime were introduced as replacements for the Date and Calendar classes.
Among them, LocalDateTime is an immutable date/time object that represents a “date and time without a timezone” and can be used in relation to databases.