Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Timestamp valueOf

Tags:

java

timestamp

I came across a code snippet like this:

Timestamp expiryDate = Timestamp.valueOf(dateStr + " " + "23:59:59.000");

Here dateStr is a string entered by the user in a form, in the format yyyy-mm-dd. Now the behavior of Timestamp.valueOf is such that it converts non-existent dates into appropriate proper dates. Say 31st June 2008 into 01st July 2008.

How can I check in Java if the dateStr string is in fact a valid date ? I know I can manually do the check, but what I would like to know is whether any method is already available to do the same.

like image 570
Vijay Dev Avatar asked Oct 22 '08 18:10

Vijay Dev


People also ask

What is timestamp format in Java?

A Timestamp also provides formatting and parsing operations to support the JDBC escape syntax for timestamp values. The precision of a Timestamp object is calculated to be either: 19 , which is the number of characters in yyyy-mm-dd hh:mm:ss. 20 + s , which is the number of characters in the yyyy-mm-dd hh:mm:ss.


1 Answers

Try SimpleDateFormat. You simply set a format such as the one in your example and then call parse on your dateStr.

like image 71
AdamC Avatar answered Sep 21 '22 00:09

AdamC