Javatpoint Logo
Javatpoint Logo

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

Test it Now

Output:

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

Example 2

Test it Now

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

Test it Now

Output:

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

Example 4

Test it Now

Output:

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





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA