sql时间datetime类型遇到的问题

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) {

    }
}

暂时好像没问题了,后续看看功能是否正常。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值