-
Date Format for Oracle..
Hi All
I am trying to update/insert a date in the Oracle DB by using the below method for the exact desired date format as per my requirement. But it is providing the date in "2010-11-01 18:29:43.717", but i need in this pattern : "2010-11-01 18:29:43 PM". means either (AM/PM) should be visible according to the time.
Can u guide me...
Code:
public void dateFormat() {
DateFormat formatter;
formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss s");
java.util.Date date = new java.util.Date();
System.out.println("Date inbetween convert : " + date);
long dateLong = (date).getTime();
Timestamp tstamp = new Timestamp(dateLong);
System.out.println("Date after convert to timestamp : " + tstamp);
//stmt.setTimestamp(5, tstamp); This is used while inserting in to DB
}
OUTPUT: Date after convert to timestamp : 2010-11-01 18:29:43.717
Thanks in Advance..
-
Code:
java.util.Date date = new java.util.Date();
DateFormat formatter= new SimpleDateFormat("dd-MM-yyyy hh:mm:ss a");
System.out.println(formatter.format(date));
-
Dear FON
Thank you very much for your quick response, i tried like this as well but the thing which we are getting is String, then i am unable to insert this String form of Date into the DB means:
Code:
stmt.setTimestamp(5, tstamp); //
stmt.setDate(5, longtype);
stmt.setObject(5, stringtype); //By using this throwing error use to_date fun().
I am using PreparedStatement for inserting a record.. can u guide for this.
-
Why does it matter what format is in DB?
You can just use java.sql.Timestamp on java side and sysdate
on oracle side to insert current date simply.
After that, use oracle functions to_char with to_date and transform inserted value in format you need.
Code:
SELECT TO_CHAR(SYSDATE, 'DD-MON-YYYY HH:MI:SS PM') FROM dual;
Those this make sense?