Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

java.sql.Timestamp and toString output

2,025 views
Skip to first unread message

Greg B

unread,
May 5, 2007, 2:34:19 AM5/5/07
to
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

Lew

unread,
May 5, 2007, 8:48:43 AM5/5/07
to

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

--
Lew

Richard Reynolds

unread,
May 5, 2007, 11:53:37 AM5/5/07
to

"Lew" <l...@nospam.lewscanon.com> wrote in message
news:-9KdnSoh0Nm24KHb...@comcast.com...

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 ...


Greg B

unread,
May 6, 2007, 12:44:55 AM5/6/07
to
On May 5, 10:48 pm, Lew <l...@nospam.lewscanon.com> wrote:
> Greg B wrote:
> > according tohttp://java.sun.com/j2se/1.5.0/docs/api/java/sql/Timestamp.html#toString(),

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

Lew

unread,
May 6, 2007, 8:30:04 AM5/6/07
to

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.

--
Lew

Richard Reynolds

unread,
May 6, 2007, 11:58:54 AM5/6/07
to

"Lew" <l...@nospam.lewscanon.com> wrote in message
news:N8ednfLah9PRV6Db...@comcast.com...

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


Richard Reynolds

unread,
May 6, 2007, 1:40:17 PM5/6/07
to

"Richard Reynolds" <richier...@ntlworld.com> wrote in message
news:2Xm%h.6879$H4....@newsfe2-gui.ntli.net...
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.

Greg B

unread,
May 6, 2007, 6:07:05 PM5/6/07
to
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.

Richard Reynolds

unread,
May 6, 2007, 8:04:06 PM5/6/07
to

"Greg B" <russia...@gmail.com> wrote in message
news:1178489225.6...@u30g2000hsc.googlegroups.com...
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.


Faton Berisha

unread,
May 9, 2007, 5:32:30 AM5/9/07
to
On May 7, 1:04 am, "Richard Reynolds" <richiereyno...@ntlworld.com>
wrote:

> 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


0 new messages