Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

java.sql.Timestamp and toString output

Thread view: 
Greg B - 05 May 2007 07:34 GMT
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 - 05 May 2007 13:48 GMT
> according to http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Timestamp.html#toString(),
>
[quoted text clipped - 15 lines]
> 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.

Signature

Lew

Richard Reynolds - 05 May 2007 16:53 GMT
>> according to
>> http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Timestamp.html#toString(),
[quoted text clipped - 18 lines]
>
> 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 ...
Greg B - 06 May 2007 05:44 GMT
> > according tohttp://java.sun.com/j2se/1.5.0/docs/api/java/sql/Timestamp.html#toString(),
>
[quoted text clipped - 20 lines]
> --
> Lew

How's 10:00:01.0 in hh:mm:ss.fffffffff format?
Lew - 06 May 2007 13:30 GMT
>>> according tohttp://java.sun.com/j2se/1.5.0/docs/api/java/sql/Timestamp.html#toString(),
>>> Returns:
[quoted text clipped - 15 lines]
>
> 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.

Signature

Lew

Richard Reynolds - 06 May 2007 16:58 GMT
>>>> according
>>>> tohttp://java.sun.com/j2se/1.5.0/docs/api/java/sql/Timestamp.html#toString(),
[quoted text clipped - 21 lines]
> 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?
Richard Reynolds - 06 May 2007 18:40 GMT
>>>>> according
>>>>> tohttp://java.sun.com/j2se/1.5.0/docs/api/java/sql/Timestamp.html#toString(),
[quoted text clipped - 23 lines]
>
> 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.
Greg B - 06 May 2007 23:07 GMT
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 - 07 May 2007 01:04 GMT
> what documentation have you been looking at, my friend. what millis?
>
[quoted text clipped - 4 lines]
> 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.
Faton Berisha - 09 May 2007 10:32 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.