This content has been marked as final. Show 5 replies
see java.util.Date - new Date()
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();
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)...
thanks jverd you've just hit the target,, and this is what i needed..
Cool, glad I could help even after two years. :-)