Joker
Joker

Reputation: 11146

ValueOf() method of java.sql.TimeStamp class, returns unwanted output

When a String value with fractional seconds is passed to java.sql.Timestamp.valueOf() method , where the fractional seconds length is less than 6, the valueOf method adds trailing zeros which changes the value of the fractional string.

public class TimestampTest {
public static void main(String[] args) {
Timestamp t = Timestamp.valueOf("2005-1-01 10:20:50.11");
}
}

valueOf - returns nano seconds as - 110000000 - which is not the original value.

This should instead add leading zeros as done in case of toString() method.

I am expecting output Just '11' or '000000011'

Can someone tell me if my understanding is right or I am mistaking some where.

Upvotes: 0

Views: 317

Answers (1)

bglow
bglow

Reputation: 61

It is a fraction so function behaves right.

0.11 = 0.110000 not 0.000011

In other words, 0.11 seconds is indeed 110,000,000 nanoseconds.

Upvotes: 2

Related Questions