Introduction
String date can be converted to a timestamp using the SimpleDateFormat, Timestamp and Date class of Java. This lab will cover two ways to convert date string to timestamp.
String date can be converted to a timestamp using the SimpleDateFormat, Timestamp and Date class of Java. This lab will cover two ways to convert date string to timestamp.
import java.text.SimpleDateFormat;
import java.sql.Timestamp;
import java.util.Date;
public class StringToTimestamp {
public static void main(String[] args) throws Exception {
// date in string format
String stringDate = "2021-01-07 02:02:16.172";
try {
// creating date format
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
// parsing string to date using parse() method
Date parsedDate = dateFormat.parse(stringDate);
// finally creating a timestamp
Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
System.out.println(timestamp);
} catch(Exception e) {
System.out.println(e);
}
}
}
To run this program, open the terminal and navigate to the ~/project/ directory.
This is another way of converting date string to timestamp using valueOf() method of java.sql.Timestamp class. This is a simple method and there is no need to do overhead coding.
import java.text.SimpleDateFormat;
import java.sql.Timestamp;
import java.util.Date;
public class StringToTimestamp {
public static void main(String[] args) throws Exception {
// date in string format
String stringDate = "2021-01-07 02:02:16.172";
try {
// converting string date to timestamp using valueOf() method
java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf(stringDate);
System.out.println(timestamp);
} catch(Exception e) {
System.out.println(e);
}
}
}
Open the terminal and navigate to the ~/project/ directory. Use the following command to compile and then run the code:
javac StringToTimestamp.java && java StringToTimestamp
If the string is in the incorrect format you will get an exception error: java.text.ParseException: Unparseable date:. To handle this error, we can add a catch block to display an error message.
import java.text.SimpleDateFormat;
import java.sql.Timestamp;
import java.util.Date;
public class StringToTimestamp {
public static void main(String[] args) throws Exception {
// date in string format
String stringDate = "2021-01-07 02:02:16";
try {
// creating date format
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
// parsing string to date using parse() method
Date parsedDate = dateFormat.parse(stringDate);
// finally creating a timestamp
Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
System.out.println(timestamp);
} catch(Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Convert epoch time to timestamps using the following code:
import java.sql.Timestamp;
public class StringToTimestamp {
public static void main(String[] args) throws Exception {
// epoch time
long epochTime = 1609934536172L;
try {
// create Timestamp object
Timestamp timestamp = new Timestamp(epochTime);
System.out.println(timestamp);
} catch(Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Convert Timestamp to string format using SimpleDateFormat.
import java.text.SimpleDateFormat;
import java.sql.Timestamp;
public class StringToTimestamp {
public static void main(String[] args) throws Exception {
// timestamp
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
try {
// create SimpleDateFormat object
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
// format Timestamp object
String formattedDate = dateFormat.format(timestamp);
// print formatted date
System.out.println(formattedDate);
} catch(Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Instead of SimpleDateFormat, we can also use the Calendar class to convert string date to timestamp.
import java.util.Calendar;
import java.sql.Timestamp;
public class StringToTimestamp {
public static void main(String[] args) throws Exception {
// string date
String stringDate = "2021-01-07 02:02:16.172";
try {
// create Calendar object
Calendar calendar = Calendar.getInstance();
// set calendar date with string date
calendar.setTime(new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS").parse(stringDate));
// convert calendar to timestamp
java.sql.Timestamp timestamp = new java.sql.Timestamp(calendar.getTimeInMillis());
System.out.println(timestamp);
} catch(Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
We can also convert string date to timestamp using the ZonedDateTime class.
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.sql.Timestamp;
public class StringToTimestamp {
public static void main(String[] args) throws Exception {
// string date
String stringDate = "2021-01-07 02:02:16.172";
try {
// create DateTimeFormatter object and specify the format of the string date
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
// create ZonedDateTime object and parse the string date
ZonedDateTime zonedDateTime = ZonedDateTime.parse(stringDate, formatter.withZone(ZoneId.systemDefault()));
// convert ZonedDateTime to timestamp
Timestamp timestamp = Timestamp.valueOf(zonedDateTime.toLocalDateTime());
System.out.println(timestamp);
} catch(Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Open the terminal and navigate to the ~/project/ directory. Use the following command to compile and then run the code:
javac StringToTimestamp.java && java StringToTimestamp
In this lab, we have learned how to convert a String date to a Timestamp in Java using different ways including SimpleDateFormat, Timestamp, Date, Calendar, and ZonedDateTime class. The SimpleDateFormat and Timestamp methods are the easiest to implement while the ZonedDateTime method is the most complicated but offers the most flexibility.