Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top