玖叶教程网

前端编程开发入门

前后台时间日期格式化,数据库查询时间不对,下次不要困扰你了

时间日期的前后台不一致问题,oracle数据库 插入和查询的时候拿到的时间和存储值不一致,今天直接上代码。

一、前台格式化代码

//将后台传过来的 日期时间 格式化方法

//显示完整的 yyyy-mm-dd hh:mm:ss

function formatime(oldDate) {

 if(!oldDate){
 return;
 }

 if(typeof(oldDate) === 'string'){

 //这里要注意你的格式是什么,这个CST不要写错了
 oldDate = oldDate.replace("CST","GMT+0800");

 var now = new Date(oldDate);

 var year = now.getFullYear();

 var month = now.getMonth()+1;

 var day = now.getDate();

 var hours = now.getHours();

 if(hours <= 9){
 hours = "0" + hours;
 }

 var minutes = now.getMinutes();
 if(minutes <= 9){
 minutes = "0" + minutes;
 }

 var seconds = now.getSeconds();
 if(seconds <= 9){
 seconds = "0" + seconds;
 }

 if(month <= 9){
 month = "0" + month;
 }

 if(day <= 9){
 day = "0" +day;
 }

 return "" + year + "-" + month + "-" + day
 + " " + hours + ":" + minutes + ":" + seconds + "";
 }
}



二、后台数据库插入问题

 oracle 数据库在你的 类中 Date 类型上面加上

1. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")

2.@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss"
)

这两个注解

3、在 oracle 插入和查询数据库的时候 xml 文件中 date 类型的去掉 jdbcType = date

就可以了。

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言