java.sql.Timestamp and toString output

G

Greg B

according to http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Timestamp.html#toString(),

Returns:
a String object in yyyy-mm-dd hh:mm:ss.fffffffff format

however, when I do

import java.sql.Timestamp;
...
Timestamp ts = new Timestamp(1000);
System.out.println("Timestamp.toString(): " + ts.toString());

I get

Timestamp.toString(): 1970-01-01 10:00:01.0 (notice the formatting of
the nanos).

if I were to do ts.setNanos(1), then my output screen would contain
the 01.00000001
 
L

Lew

Greg said:
according to http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Timestamp.html#toString(),

Returns:
a String object in yyyy-mm-dd hh:mm:ss.fffffffff format

however, when I do

import java.sql.Timestamp;
..
Timestamp ts = new Timestamp(1000);
System.out.println("Timestamp.toString(): " + ts.toString());

I get

Timestamp.toString(): 1970-01-01 10:00:01.0 (notice the formatting of
the nanos).

if I were to do ts.setNanos(1), then my output screen would contain
the 01.00000001

Great. You verified the Javadocs. Looks like they were right.
 
R

Richard Reynolds

Lew said:
Great. You verified the Javadocs. Looks like they were right.

bit inconsistent though isn't it, if it was being treated as a numeric value
you'd expect
0 or 1
if it was being treated as a formatted string for output you'd expect
00000000 or 00000001
I don't see why it would do 0 in one case and 00000001 in another

disclaimer - I haven't tried it myself ...
 
L

Lew

Greg said:
How's 10:00:01.0 in hh:mm:ss.fffffffff format?

As I understand that format string, it indicates that up to nine (?)
fractional digits will show, but it is usual in formatted output to show fewer
fractional digits when that is sufficient to represent the value. I see no
problem.
 
R

Richard Reynolds

Lew said:
As I understand that format string, it indicates that up to nine (?)
fractional digits will show, but it is usual in formatted output to show
fewer fractional digits when that is sufficient to represent the value. I
see no problem.

so does it do: 10:00:01.1 or 10:00:01.00000001 like the OP claimed?
 
R

Richard Reynolds

Richard Reynolds said:
so does it do: 10:00:01.1 or 10:00:01.00000001 like the OP claimed?
just tried it, first thing to note is they're not nanos, the way the
original poster constructed it they're millis, but for 2 timestamps created
using 1000 and 1001 millis:
Timestamp.toString(): 1970-01-01 01:00:01.0

Timestamp.toString(): 1970-01-01 01:00:01.001

so, yes, inconsistency abounds!

if I do setNanos(0) and setNanos(1) on the same timestamp:

Timestamp.toString(): 1970-01-01 01:00:01.0

Timestamp.toString(): 1970-01-01 01:00:01.000000001

so again, inconsistent, bug I'd say.
 
R

Richard Reynolds

Greg B said:
what documentation have you been looking at, my friend. what millis?

http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Timestamp.html#toString()

toString
public String toString()Formats a timestamp in JDBC timestamp escape
format. yyyy-mm-dd hh:mm:ss.fffffffff, where ffffffffff indicates
nanoseconds.
Same docs Greg however I'm reading between the lines a bit! think they may
have left a bit out of the toString description.

The constructor takes a single long that says it specifies the time in
millis and when I only use that I get a telltale 3 places after the seconds
(if it's not 0):
Timestamp.toString(): 1970-01-01 01:00:01.0
Timestamp.toString(): 1970-01-01 01:00:01.001

However if I do a setNanos(0) and setNanos(1) on it then I get a telltale 9
places:
Timestamp.toString(): 1970-01-01 01:00:01.0
Timestamp.toString(): 1970-01-01 01:00:01.000000001

so I'm guessing that if you don't explicitly set the nanos it acts as if
it's got millis only, and it certainly does the formatting differently
depending on if it's 0 or not in either case.
 
F

Faton Berisha

The constructor takes a single long that says it specifies the time in
millis and when I only use that I get a telltale 3 places after the seconds
(if it's not 0):
Timestamp.toString(): 1970-01-01 01:00:01.0
Timestamp.toString(): 1970-01-01 01:00:01.001

As you should.
However if I do a setNanos(0) and setNanos(1) on it then I get a telltale 9
places:
Timestamp.toString(): 1970-01-01 01:00:01.0
Timestamp.toString(): 1970-01-01 01:00:01.000000001

Again, as you should.
so I'm guessing that if you don't explicitly set the nanos it acts as if
it's got millis only, and it certainly does the formatting differently
depending on if it's 0 or not in either case.

The precise description would be ss.fffffffff indicate the seconds.
I guess, it is implicitly understood in the documentation that the
format fffffffff
behaves as fractional one (implying a standard behaviour for
fractional digits).

Regards,
Faton Berisha
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top