I don't know why, but each time I try to save a LocalDateTime
to MySQL then later try to view it, 7 hours have been subtracted from it. For example, when I put in LocalDateTime.of(2018, 6, 28, 18, 30)
and then save it when I get it back out of the database, it says 2018-06-28 11:30
. Now, I get that the reason it is doing this is because I live in Seattle which is in GMT-7. What I don't know is how to get it to stop doing that. I'm guessing this has a very simple solution, but I can't seem to figure it out.
--EDIT--
Here is the code I am using to save to the database.
public static int update(String tableName, String idColumnName, int id, Map<String, Object> newValues) {
String sql = "UPDATE " + tableName + "\n"
+ "SET lastUpdate = " + convertDateToDBString(LocalDateTime.now()) + ",\n"
+ "lastUpdateBy = '" + activeUser.getUsername() + "'";
for (Map.Entry<String, Object> newValue : newValues.entrySet()) {
sql += ", \n" + newValue.getKey() + " = " + convertObjectToDBString(newValue.getValue());
}
sql += "\nWHERE " + idColumnName + " = " + id + ";";
if (connected) {
try {
printSQL(sql);
return statement.executeUpdate(sql);
} catch (SQLException e) {
printError(1111, "Could not run UPDATE query", e);
}
}
return -1;
}
Here is the code I am using to draw from the database.
public static ResultSet query(String sql) {
if (connected) {
try {
printSQL(sql);
if (statement.execute(sql)) return statement.getResultSet();
} catch (SQLException e) {
printError(1110, "Could not run SELECT query", e);
}
}
return null;
}
I don't think this will really help solve the issue which is why I didn't bother adding it in the first place, but you guys are asking for it.
-- EDIT --
Here is the code for convertDateToDBString
.
public static String convertDateToDBString(LocalDateTime datetime) {
String dateString = "'" + Timestamp.valueOf(datetime) + "'";
return dateString;
}