Java 前后台传递 时间问题(yyyy-MM-dd HH:mm:ss)

版权声明:UU小七 :转载请标注哦!^v^ https://blog.csdn.net/qq_36474549/article/details/84136176

一:后台时间

1. 给定义的属性插入 new Date

new Date()  //对应的时间格式 Fri Nov 16 11:16:17 CST 2018

2. 取出 Fri Nov 16 11:16:17 CST 2018 格式数据转换为 yyyy-MM-dd HH:mm:ss 格式

 String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())';
 //new Date 可以替换为取出的 Date 类型数据

3.直接获得 yyyy-MM-dd HH:mm:ss 时间

 String time = LocalDate.now()+" "+ LocalTime.now().withNano(0);
二:前台时间问题:

1.获得的如下时间格式

Date.prototype.Format = function (fmt) { //fmt 为毫秒
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

2.获得如下时间格式:

function getMyDate(str) {  //str 为毫秒数
    var oDate = new Date(str),
        oYear = oDate.getFullYear(),
        oMonth = oDate.getMonth() + 1,
        oDay = oDate.getDate(),
        oHour = oDate.getHours(),
        oMin = oDate.getMinutes(),
        oSen = oDate.getSeconds(),
        oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay) + ' ' + getzf(oHour) + ':' + getzf(oMin) + ':' + getzf(oSen);//最后拼接时间
    return oTime;

    //补0操作
    function getzf(num) {
        if (parseInt(num) < 10) {
            num = '0' + num;
        }
        
        return num;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_36474549/article/details/84136176