mysql数据库里设置的时间为datetime类型的,时间存入数据库时为包括时分秒的时间格式,结果查出来为只有日期而时分秒都为00:00:00,如下
上为数据库,下为控制台打印时间
下为mapper配置文件:
很奇怪,结果最后发现是mybatis的mapper配置文件中时间字段的jdbctype写成了DATE,改成TIMESTAMP就能正常显示了。如下
还有,之前时间查询使用Calender类进行时间范围的获取:
public static TimeView getTime(Date from , Date to){//Date from ,Date to
TimeView timeView = new TimeView();
Map<String,Date> map = new HashMap<>();
Calendar calendar = new GregorianCalendar();
calendar.setTime(from);
calendar.set(Calendar.HOUR,0);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.MILLISECOND,0);
//calendar.add(Calendar.HOUR,-12);
timeView.setFrom(calendar.getTime());
calendar.setTime(to);
calendar.set(Calendar.HOUR,23);
calendar.set(Calendar.MINUTE,59);
calendar.set(Calendar.SECOND,59);
calendar.set(Calendar.MILLISECOND,999);
//calendar.add(Calendar.HOUR,-12);
timeView.setTo(calendar.getTime());
return timeView;
}
时间一直会发生变化,有时候是正常,有时候就往前推了12小时,
后来设置了时间配置:
@Configuration
public class TimeZoneConfiguration implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.setProperty("user.timezone", "Asia/Shanghai");
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
暂时好像没问题了,后续看看功能是否正常。