To convert date into GMT+00:00, and store in db, and conver back date
843854Nov 10 2001 — edited Dec 1 2001To 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