Mapping of Timestamp with Oracle Date datatype

Posted on 2000-04-02
Medium Priority
Last Modified: 2012-06-27
 we just have tried to capture the time when user enters the system and this field is stored in the database. here how we do it :

new Timestamp (System.currentTimeMillis ());

this timestamp when we are trying to store in oracle database whose field is of type Date it gives some error as field length is too large.

As timestamp is subclass of date class it should map to date type of oracle datatype.

Kindly help me why is it giving such problems and also is there any way i can store time in milli seconds in oracle database ?

Question by:pravin_tiwari
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3

Expert Comment

ID: 2678849
I'm guessing the Oralce date class is in the format mm/dd/yyyy?

if so you'll want to store the following method call in your Oracle DB..

DateFormat.getDateInstance(DateFormat.SHORT, Locale.US)

DateFormat is declared in java.text, and getDateInstance is a static member.

If you are not using the miliseconds anywhere other than inside your Java code, why couldn't you just store your miliseconds as a STRING in the DB?

Author Comment

ID: 2678896
  we have changed the date format of oracle database . point is whether i can map Timestamp class of java to Date datatype of Oracle ? if yes, what all settings are required.
as i apprecaite your idea of storing milli seconds as separate field but right now i will result in lots of client side code changes .

thanks anyway

Expert Comment

ID: 2678985

 why not try having in your database of type Timestamp?
 The timestamp datatype is of both time and date.

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users


Expert Comment

ID: 2678998
pravin i think the problem is that u can not assign timestamp directly to the date field in oracle as timestamp contains date aswell as time that is why i think it gives u the error as  field length is too large.
so why dont u use sql.date class  it self
Date d = new Date(System.currentTimeMillis ());
and then insert it into ur date field

Author Comment

ID: 2679004
hi rjackman,
    But will it contain the time field also ? becoz this field is my primary key and i want to see to it the it is unique ...now if it has only date field then it might voilate the primary key as it is possible that 2 user will enter my system on same date or may be sam second ! so probability that this field will be same for 2 user if date filed include's milliseconds is rare and that is the reason i want to insert milliseconds fiels also !

thanks anyways
LVL 16

Expert Comment

ID: 2679116
java.sql.Date contains onle Date information.

you can use LONGINT database type and save the milliseconds directly.

Accepted Solution

azawawi earned 300 total points
ID: 2679760
Well this is a weird thing in Java to know that java.sql.Date doesnt store SQL TIMESTAMP type. In fact, java.sql.Date truncates the timestamp part of the DATE. So your only option is to use java.sql.Timestamp instead to correctly update a TIMESTAMP field (or Oracle DATE type-field).

i did that and everything went smoothly. Here is some sample code:

// ...do Oracle driver classs loading...
// ...try to connect to DB...

// get today's timestamp and update the corresponding field in the Database
java.sql.Timestamp today = new java.sql.Timestamp(System.currentTimeMillis());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sql = "UPDATE sometable SET timestamp = to_date(" + tr(df.format(today)) + ",'yyyy-mm-dd hh24:mi:ss')";

// ...free DB resources in reverse order...

for more information on java.sql.Timestamp see http://java.sun.com/products/jdk/1.1/docs/api/java.sql.Timestamp.html

Any more questions dont hesitate.


Expert Comment

ID: 2679766
no u can not save time in date field u can refer hey hey's comment then it will work for u..

Expert Comment

ID: 2679833
you mean add an execute Update and all those little thingies? :)

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

777 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