I have 2 columns in my table with data type TIMESTAMP and DATE. The column with data type "DATE" is default date/time, so that we don't need to insert from JAVA, It automatically gets inserted. For the other column with data type "TIMESTAMP" I manually insert the current time from JAVA which is optional. The actual problem is the database time differs from the time which I insert from java. ie.The default time inserted is not same as the date/time which I manually inserted from DB. Is there any way to match the database time to system time which is inserted from java? The SQL date differs by 7-8 minutes from my system date, which should get matched while inserting it manually.
-
SO you are saying you want TIMESTAMP and DATE columns have same values at the end of insertions. That is current date and time? – R Dhaval Jun 02 '17 at 09:55
-
No. The date which I inserted manually from java and the date which was inserted by default from SQL aren't same. – Kishor kumar R Jun 05 '17 at 05:18
-
Please provide your current dates the you are getting/setting and the dates that you want, in the question itself. – R Dhaval Jun 05 '17 at 05:21
-
If the db data/time is 1:00 PM and my system date/time is 1:07 PM. – Kishor kumar R Jun 05 '17 at 07:29
-
is your db on local system? Is your system's and db's timezone are same? – R Dhaval Jun 05 '17 at 13:51
-
No they aren't same. If they are same I'll not get this problem – Kishor kumar R Jun 05 '17 at 14:12
-
I have updated the answer. – R Dhaval Jun 05 '17 at 14:32
1 Answers
If you want to insert current_date in both mentioned columns and you are unable to do so:
Check if your system's (where java code is running) timezone and database's timezone are same.
In database run this query:
SELECT dbtimezone FROM DUAL;
and if you find it different from your java/system then you can always change it using :
ALTER DATABASE SET TIME_ZONE='+05:30';
https://docs.oracle.com/cd/E11882_01/server.112/e10729/ch4datetime.htm#NLSPG263
EDIT 1: If you don't want to change the timezone you can do two things:
1) select the date/time form database in java instead of taking it from Calendar object. Then insert the same date/time to TIMESTAMP column. display timestamp value in java from database
2) set database DATE field from java instead of database itself. So apparently setting both the fields from java. http://alvinalexander.com/java/java-timestamp-example-current-time-now

- 536
- 1
- 9
- 21
-
I should not change the system or db time as i could deploy the same project in some other environment. But database date should gets matched with the environment date. Is that possible? – Kishor kumar R Jun 05 '17 at 05:20
-
yeah this is one solution. But considering the performance don't we have any other options? – Kishor kumar R Jun 05 '17 at 15:12