【发布时间】:2017-11-05 08:57:26
【问题描述】:
我在服务器上使用 MySQL 数据库。我的 Web 应用程序服务器位于 US/Chicago,但我希望将 DateTime 值保存在 Africa/Lagos 时间。
我从 Stack Overflow 获得了这段代码的一部分,但是当我更新一条记录时,它仍然在数据库记录中显示芝加哥时间。 请问我在这里做错了什么?
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;
}
}
【问题讨论】:
标签: java mysql date calendar timezone