I am using MySQL database on the Server. My Web application Server is in US/Chicago but I want my DateTime values to be saved in Africa/Lagos time.
I got part of this code here from Stack Overflow but when I update a record it still shows Chicago time in the database record. Please what am I doing wrong here?
public static boolean saveImageFileName(int id, String fileName) throws SQLException, IOException, IllegalArgumentException, ClassNotFoundException
{
try(Connection conn = Config.getDatabaseConnection())
{
//Create a timezone object based on Africa/Lagos time zone
SimpleTimeZone timeZone = new SimpleTimeZone(1, "Africa/Lagos");
//Create a calendar object using the timezone object
GregorianCalendar calendar = new GregorianCalendar(timeZone);
//Create a timestamp object using the calendar's date and time
Timestamp timestamp = new Timestamp(calendar.getTime().getTime());
//Create an SQL query String
String sql = "UPDATE `requests` SET `filename` = ?, `uploaded_date` = ? WHERE `id` = ?";
//Create a prepared statement object from database connection
PreparedStatement pst = conn.prepareStatement(sql);
//Set prepared statement parameters
pst.setString(1, fileName);
pst.setTimestamp(2, timestamp, calendar); //Set TimeStamp and Calendar
pst.setInt(3, id);
//Update the record
int update = pst.executeUpdate();
//return update result
return update == 1;
}
}