【发布时间】:2021-09-09 17:08:40
【问题描述】:
我选择了一个时间,例如我输入了 Thu Jan 01 16:00:00 WET 1970 以及一个时区,例如 GMT+1:00。我的意思是我想用这个时区记录这个日期(gmt+1 已经是 16:00)。但在保存模型后,该值将在数据库中保存为 Thu Jan 01 15:00:00 WET 1970。
服务器位于非洲/卡萨布兰卡时区。
我尝试将此日期转换为我想要的带有时区的字符串,它显示了正确的时间,但是当我尝试将此字符串转换为日期时,我仍然收到少一小时的时间。
DisplayedTimeZone displayedTimeZone = configuration.getDisplayedTimeZone();
if(displayedTimeZone != null){
Date orderCuttOffTime = configuration.getCutOffTime(); // Thu Jan 01 15:00:00 WET 1970
if(orderCuttOffTime != null){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
sdf.setTimeZone(TimeZone.getTimeZone(displayedTimeZone.getCode()));
String closingTime = sdf.format(orderCuttOffTime); // 1970-01-01 16:00:00 +0100
try {
configuration.setCutOffTime(dateFormat.parse(cuttOffTime)); // returns again Thu Jan 01 15:00:00 WET 1970 and not 16:00:00
}catch (ParseException e) {
e.printStackTrace();
}
}
}
甚至 cuttofftime 包含我想保存在数据库中的时间 Thu 1970-01-01 16:00:00 +0100 dateFormat.parse(cuttOffTime) 返回值 Thu Jan 01 15:00:00 WET 1970
我想在解析期间向我发送一个与字符串中相同时间的日期
【问题讨论】: