2

I want to insert the current date to my oracle database Bills. I am getting the date from system using

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();

is that possible to directly insert it into the database without converting to the default format (dd-month-yyyy) of DATE data type in Oracle?

2
  • Why not just use CURRENT_DATE as a value literal in your SQL? insert into foo (some_date_column) values (current_date) Commented Oct 27, 2013 at 14:57
  • @a_horse_with_no_name - You should probably use SYSDATE, not CURRENT_DATE. SYSDATE is the date on the server while CURRENT_DATE is the date on the client. I'd imagine you almost always want to insert the server's date into the database, not anything else. Commented Dec 13, 2016 at 21:49

3 Answers 3

13

Just in case you need CURRENT date, use:

INSERT INTO YOUR_TABLE (YOUR_DATE_COLUMN, COLUMN_2, COLUMN_3) 
VALUES (SYSDATE, 1234, 567);
Sign up to request clarification or add additional context in comments.

1 Comment

What's the difference between SYSDATE and CURRENT_DATE as the person who commented on the question suggested? Edit: Found the answer. SYSDATE is the date on the server while CURRENT_DATE is the date on the client. So SYSDATE is probably almost always the desired one, I'd think.
5

You did not give any context on what interface you are using to communicate with the DB. Assuming plain JDBC, let the driver handle the problem, use a statement object and set the parameter(s) properly:

Connection connection = ...
PreparedStatement statement = connection.prepareStatement("INSERT INTO <TableNameHere> VALUES (?)");
statement.setObject(1, new java.sql.Date());
statement.executeUpdate();

(Error handling and fluff ommited) The JDBC driver will deal with the details on how to format the date as the database wants it. Don't even worry about it.

2 Comments

More than that, Oracle DB DATE datatype doesn't even have a format.. Sending a String will cause the DB to implicitly convert the String according to NLS_DATE_FORMAT which might be different in some environments
If there is a session timezone set, does it write the date in that timezone or always UTC?
0

1) It is not mandatory to use the default dd/MM/yyyy format in while initializing SimpleDateFormatter. You have a list of options which can match to your requirement. SimpleDateFormat

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.