Solved

Getting Microseconds from Date

Posted on 2006-06-14
16
756 Views
Last Modified: 2008-01-09
Hi, we hold timestamps in our datavase in the following format: 2004-02-13:10:28:45.530374, trouble is that when we try to use  simpleDateFormat with the format yyyy-MM-dd hh:mm:ss.SSSSSS it doesnt work since it only goes upto milliseconds not microseconds.

Is there a way I can get the microseconds?
0
Comment
Question by:Chriseybaby
  • 4
  • 3
  • 2
  • +3
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16901116
Microseconds are not supported in Java dates
0
 
LVL 14

Expert Comment

by:StillUnAware
ID: 16901120
That's a good one, 0 minutes in answering a question :)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16901200
The best I know is System.nanoTime () ; but that's nanoseconds.... the Date class does not support micro-seconds.
0
 
LVL 92

Expert Comment

by:objects
ID: 16901223
0
 
LVL 4

Accepted Solution

by:
Yagantappa earned 250 total points
ID: 16901227
You can make use of java.sql.Timestamp.
0
 

Author Comment

by:Chriseybaby
ID: 16903319
Thanks for your help, looks like its going to have to be the timestamp, very annoying tho...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16903537
>>looks like its going to have to be the timestamp

That won't work either. Timestamp only supports milliseconds
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Expert Comment

by:Mayank S
ID: 16910404
TimeStamp has a method called getNanos () but that wouldn't get the microsecond component.
0
 
LVL 14

Expert Comment

by:StillUnAware
ID: 16911057
What are You talking about, not a single OS internally can count the time so exactly, I guess the shortest period of time which can be accessed is milisecond.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16911135
Well I haven't used the getNanos () method of TimeStamp but probably that is what Yagantappa meant. In any case, like I said - it will not solve the problem because whether it gets the nanoseconds or not, it cannot get the microseconds. System.nanoTime () says "Returns the current value of the most precise available system timer, in nanoseconds".
0
 
LVL 4

Expert Comment

by:Yagantappa
ID: 16912338
Not all databases support micro seconds. Oracle 10g support micro seconds. I tried the following program and it worked fine

Table in Oracle database
CREATE TABLE TEST_TIMESTAMP (
    T1      TIMESTAMP(6) NULL
    )
GO

Here is the test java code and its output

        Timestamp ts = new Timestamp(106, 06, 10, 10, 10, 10, 213286789);
        System.out.println("ts = " + ts);
        Class.forName ("oracle.jdbc.driver.OracleDriver");
        Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@xxx.xxx.xxx.xxx:xxxx:xxxX", "xxxxxx", "xxxxxx");
        Statement stmt = conn.createStatement();
        PreparedStatement p = conn.prepareStatement("update test_timestamp set t1 = ?");
        p.setTimestamp(1, ts);
        p.execute();
        ResultSet rs = stmt.executeQuery("select * from test_timestamp");
        while (rs.next())
        {
            System.out.println("column 1 = " + rs.getTimestamp(1));
        }

Output
--------
ts = 2006-07-10 10:10:10.213286789
column 1 = 2006-07-10 10:10:10.213287
column 1 = 2006-07-10 10:10:10.213287
column 1 = 2006-07-10 10:10:10.213287
column 1 = 2006-07-10 10:10:10.213287
column 1 = 2006-07-10 10:10:10.213287
0
 

Author Comment

by:Chriseybaby
ID: 16912368
I havent tried using it yet, just hoped it would work.

The problem is, after I get the data from the data base I lose the last little bit of the timestamp. Therefore when I try to compare the 2 they are different. I could use a like query but would prefer to find a solution.

Maybe if I save it as a string I might be able to get around it...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16912402
>>I tried the following program and it worked fine

That's all OK but Chriseybaby's question specifies that s/he is using it in conjunction with Date. That's not possible, as Date only supports ms
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16912408
>> I havent tried using it yet, just hoped it would work.

You shouldn't close the question until you've confirmed that the answer is correct :) many people will have many suggestions and you can't say which one is going to work until you try them. Anyway, if this one does, its great :)
0
 
LVL 92

Expert Comment

by:objects
ID: 16916529
Timestamp should work fine.
If the column in the database is a timestamp that supports the accuracy you need, then use getTimestamp() to retrieve it.
If its stored as a string then split the string (d.split("\\.")) when you extract it and parse the regular date/time (2004-02-13:10:28:45) with SDF and store it in a Timestamp, and parse the rest (530374) as an int and set the nanoseconds on your parsed Timestamp using setNanos() method.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Running Jira on Raspberry PI 2? 3 261
compre toata in where clue oracle 4 56
print map entry 34 55
factorial example 4 34
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now