如何根据时区将时间戳存储在 mysql 中?
How to store timestamp in mysql based on Timezone?
我想在我的登录 table 中存储 last_login 的时间戳。我在我的 DAO 层中创建时间,并在用户登录时在 table 中更新它。问题是,我想根据时区更新它。我的密码是
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
final TimeZone utc = TimeZone.getTimeZone("IST");
formatter.setTimeZone(utc);
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 0); // number of days to add
String date = (String) (formatter.format(c.getTime()));
Date convertedDate = formatter.parse(date);
Date timestampObj = new Timestamp(convertedDate.getTime());
这只是返回服务器的时间戳。如果服务器在英国,则时间戳是英国的。
我知道 mysql 时间戳以 UTC 格式存储时间。但不是在 IST 节省时间,而是在英国节省时间。
我该如何进行?
据我了解,您需要验证服务器时间。
检查您的操作系统时间是否设置为 IST(UTC 偏移量:UTC +5:30)。
我想在我的登录 table 中存储 last_login 的时间戳。我在我的 DAO 层中创建时间,并在用户登录时在 table 中更新它。问题是,我想根据时区更新它。我的密码是
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
final TimeZone utc = TimeZone.getTimeZone("IST");
formatter.setTimeZone(utc);
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 0); // number of days to add
String date = (String) (formatter.format(c.getTime()));
Date convertedDate = formatter.parse(date);
Date timestampObj = new Timestamp(convertedDate.getTime());
这只是返回服务器的时间戳。如果服务器在英国,则时间戳是英国的。 我知道 mysql 时间戳以 UTC 格式存储时间。但不是在 IST 节省时间,而是在英国节省时间。 我该如何进行?
据我了解,您需要验证服务器时间。
检查您的操作系统时间是否设置为 IST(UTC 偏移量:UTC +5:30)。