Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

To convert date into GMT+00:00, and store in db, and conver back date

843854Nov 10 2001 — edited Dec 1 2001
To covert Application Server Date&Time to GMT +00:00 and store in database, and retrive Date&Time from database, covert based on user Timezone, here is the solution for you............

To covert Application Server Date&Time to GMT +00:00 and store in database:

Date actualDate = new Date();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");

TimeZone tz = TimeZone.getTimeZone("GMT");

sdf.setTimeZone(tz);

String str = sdf.format(actualDate);

Timestamp timestamp = Timestamp.valueOf(str);

String query1 = "insert into testgmt values(?)";

PreparedStatement ps = connection.prepareStatement(query1);
ps.setTimestamp(1,timestamp);
ps.executeUpdate();


Retrive Date&Time from database, covert based on user Timezone:

String query2 = "select dd from testgmt";
ResultSet rs = statement.executeQuery(query2);
Timestamp timestamp = null;
String strDate = "";

Calendar calendar = new GregorianCalendar(
TimeZone.getTimeZone("Asia/Calcutta"));
long num = 0;

while(rs.next()) {
timestamp = rs.getTimestamp(1);

calendar.setTime(timestamp);
num = calendar.get(Calendar.ZONE_OFFSET)+calendar.get(Calendar.DST_OFFSET);

Date convertedDate = new Date(timestamp.getTime()+num);
} //while end


import the following classes;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;

import java.sql.Timestamp;

import java.text.TimeZone;
import java.text.SimpleDateFormat;


cheers
Nalluri
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 29 2001
Added on Nov 10 2001
1 comment
184 views