5 Replies Latest reply on Jul 21, 2007 4:36 PM by 796440

    How to get a current date timestamp ?

    807587
      Hi,

      I need to get the current date timestamp in a format DDMMYY HH:MM:SS.mmmm and store it in an Oracle column which is declared as of type DATE.

      Also i need to store this timestamp in a string format.

      Please give your inputs in this regard, which is appreciated.

      Regards
        • 1. Re: How to get a current date timestamp ?
          807587
          see java.util.Date - new Date()
          see java.text.SimpleDateFormat
          • 2. Re: How to get a current date timestamp ?
            796440
            Dates dont' have formats. String represenations of dates do.

            If you have a DB column that is of char or varchar type but represents a date/time, you should change your data model so that the column type is date or time or timestamp. If you can't do that, and need to put a date into a particular format, use java.text.SimpleDateFormat.

            To get the current date and time, look at System.currentTimeMillis, java.util.Date, java.sql.Date, java.sql.Timestamp.

            To put one of these into a database, use PreparedStatement. For example, a timestamp would be something like this:
            PreparedStatement ps = con.prepareStatement("insert into some_table (some_timestamp) values(?)");
            Timestamp ts = new TimeStamp(...);
            ps.setTimestamp(1, ts); 
            ps.excuteUpdate();
            • 3. Re: How to get a current date timestamp ?
              807587
              When writing to the Oracle DATE column make sure you use an instance of: java.sql.Timestamp otherwise the Oracle driver will truncate the time part... I've been caught out a couple of times with that ;) And use ResultSet.getTimestamp when reading it back...

              I wouldn't ever store the date/time as a string, it just mucks things up and is a pain to maintain, and you lose the ability to use some of the Oracle time functions, unless you do an explicit TO_DATE (which is still a pain cause you'll generally have to specify the format anyway)...
              • 4. Re: How to get a current date timestamp ?
                807592
                thanks jverd you've just hit the target,, and this is what i needed..
                system.out.println("best regards");
                • 5. Re: How to get a current date timestamp ?
                  796440
                  Cool, glad I could help even after two years. :-)