I want to insert current date and time in the format "yyyy-mm-dd hh:mm:ss" using Java.
Which datatype is suitable for this, and which method of PreparedStatement
do I need to use?
-
why not just use `sysdate` instead of passing it from Java – Gurwinder Singh Sep 20 '17 at 04:34
-
How to use SYSDATE with preparedstatement? – Nidheesh Sep 20 '17 at 04:41
-
See my answer below – Gurwinder Singh Sep 20 '17 at 04:44
-
1It’s a very broad question as it stands. Did you do any search or research prior to asking? I suspect you could have got a much more elaborate answer faster that way. – Ole V.V. Sep 20 '17 at 05:10
3 Answers
I assume the datatype of the column is DATE in the database. If you want to insert the current date and time, you can either use oracle's built in SYSDATE
:
ps = conn.prepareStatement('insert into your_table(date_col, ...) values (sysdate, ...)');
Or parameterize it and use setTimestamp() on PreparedStatement:
ps = conn.prepareStatement('insert into your_table (date_col, ...) values (?, ...)');
ps.setTimestamp(1, new java.sql.Timestamp(System.currentTimeMillis()));
As suggested by @Basil in the comments below, You can also use java.time.Instant
class:
ps.setObject(1 , Instant.now());
and while retrieving , use ResultSet#getObject(int/String, Class<T>)
:
Instant i = rs.getObject(1, Instant.class);

- 38,557
- 6
- 51
- 76
-
I checked with second case. It stores value in the format "yyyy-mm-dd 00:00:00". – Nidheesh Sep 20 '17 at 04:52
-
-
1I haven’t checked, but I would certainly expect a newer Oracle JDBC driver to accept an `Instant` object rather than an outdated `Timestamp`. If so, I recommend taking advantage of it. – Ole V.V. Sep 20 '17 at 05:12
-
1This Answer is using troublesome old date-time classes that are now legacy, supplanted by the java.time classes. – Basil Bourque Sep 20 '17 at 05:21
tl;dr
which method of PreparedStatement do I need to use
Storing:
PreparedStatement::setObject( … , java.time.Instant myInstant )
Retrieving:
ResultSet::getObject( … , Instant.class )
java.time
The modern approach uses java.time classes. Specifically: PreparedStatement::setObject
and ResultSet::getObject
Get the current moment in UTC by calling Instant.now()
.
myPreparedStatement.setObject( … , Instant.now() ) ;
Retrieve that value.
Instant instant = myResultSet.getObject( … , Instant.class ) ;
Store that in a column of type akin to SQL-standard type TIMESTAMP WITH TIME ZONE
.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.

- 303,325
- 100
- 852
- 1,154
You can get your expected date time format using java.util.Date
class . If you want to store only date then you have to convert java.util.date
to java.sql.date
..
If you want to store all that is time and date then go for string ( i.e. varchar data type in Oracle table) ..

- 364
- 3
- 10
-
I recommend staying away from the long outdated `Date` classes mentioned. Better to use the modern Java date and time API, for example an `Instant`. This will also save you from converting between two different `Date` types. – Ole V.V. Sep 20 '17 at 05:13