Java Timestamp valueOf() Method

The valueOf() method of Timestamp class converts the string object to Timestamp value or obtains an instance of Timestamp from a LocalDateTime object.

Syntax

Parameters

Here parameter 's' and 'dateTime' represent a string value in date-time format and a LocalDateTime value .

Returns

The valueOf() method of Timestamp class returns a Timestamp value.

Throws

The valueOf () method of Timestamp class throws:

IllegalArgumentException- if the given string argument is not in date-time(yyyy-mm-dd hh:mm:ss) format.

NullPointerException- if datetime is null.

Example 1

Output:

String : 2018-09-01 09:01:15
value of Timestamp : 2018-09-01 09:01:15.0

Example 2

Output:

String : 2018-09-01
Exception in thread "main" java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
	at java.sql.Timestamp.valueOf(Timestamp.java:204)
	at JavaTimestampValueOfExample2.main(JavaTimestampValueOfExample2.java:7)

Example 3

Output:

Exception in thread "main" java.lang.NullPointerException
	at java.sql.Timestamp.valueOf(Timestamp.java:551)
	at JavaTimestampValueOfExample3.main(JavaTimestampValueOfExample3.java:8)

Example 4

Output:

value of Timestamp : 2018-09-06T12:04:44.386