可以看目录,定位查看各个小节

java后台转换时间格式


使用

String pattern = "yyyy-MM-dd HH:mm:ss";//要转换的时间格式
Date time = new Date();//也可以是new Date().getTime()毫秒类型
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String date = formatter.format(time);

以上就可以转换成任意后台想要显示的时间格式。


注意:为什么这里java转一遍时间格式,为什么又要在js中转换一次时间格式?因为从java传输json格式的数据到前端,显示的格式就会是有问题的。

例如:当后台转换格式成:2017-10-10 10:10:10 这个时间字符串时,传输到前台就只有2017-10-10这样的数据。这就因为是因为空格的问题,但是我们就是需要这样的时间格式,那么就不应该从后台传输这样的时间格式到前台。直接传输毫秒类型的时间格式,然后在前台通过js进行格式转换即可。


js前端转换时间格式

首先要知道前端的时间获取

new Date():获取当前时间,一种不常见的时间格式。需要转换格式

new Date().getTime();获取毫秒时间。

new Date("2017-10-10 10:10:10");把时间字符串转换成时间类型

一个小案例:

<script>
    var time = 0;
    time = new Date("2017-10-10 10:10:10");
    console.log(time);

    time = new Date("2017-10-10 10:10:10").getTime();
    console.log(time);

    time = new Date("2017-10-10").getTime();
    console.log(time);

    time = new Date("10:10:10").getTime();//非法时间转换
    console.log(time);

    time = new Date(1507601410000);//毫秒转日期格式
    console.log(time);
</script>




后台打印:

java 实体里的时间字段怎么设置自动生成日期 java怎么设置时间格式_javascript


然后如何转换成我们需要的日期格式呢?

先略微的说一下Date的原型。

Date 构造函数的原型

参考地址:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date/prototype

 

该参考网址也给出了日期类型Date的很多方法如set和get方法。这些方法就可以提供一些简单的日期格式化

Getter

Date.prototype.getDate()
    根据本地时间返回指定日期对象的月份中的第几天(1-31)。
Date.prototype.getDay()
    根据本地时间返回指定日期对象的星期中的第几天(0-6)。
Date.prototype.getFullYear()
    根据本地时间返回指定日期对象的年份(四位数年份时返回四位数字)。
Date.prototype.getHours()
    根据本地时间返回指定日期对象的小时(0-23)。
Date.prototype.getMilliseconds()
    根据本地时间返回指定日期对象的微秒(0-999)。
Date.prototype.getMinutes()
    根据本地时间返回指定日期对象的分钟(0-59)。
Date.prototype.getMonth()
    根据本地时间返回指定日期对象的月份(0-11)。
Date.prototype.getSeconds()
    根据本地时间返回指定日期对象的秒数(0-59)。
Date.prototype.getTime()
    返回从1970-1-1 00:00:00 UTC(协调世界时)到该日期经过的毫秒数,对于1970-1-1 00:00:00 UTC之前的时间返回负值。
Date.prototype.getTimezoneOffset()
    返回当前时区的时区偏移。
Date.prototype.getUTCDate()
    Returns the day (date) of the month (1-31) in the specified date according to universal time.
Date.prototype.getUTCDay()
    Returns the day of the week (0-6) in the specified date according to universal time.
Date.prototype.getUTCFullYear()
    Returns the year (4 digits for 4-digit years) in the specified date according to universal time.
Date.prototype.getUTCHours()
    Returns the hours (0-23) in the specified date according to universal time.
Date.prototype.getUTCMilliseconds()
    Returns the milliseconds (0-999) in the specified date according to universal time.
Date.prototype.getUTCMinutes()
    Returns the minutes (0-59) in the specified date according to universal time.
Date.prototype.getUTCMonth()
    Returns the month (0-11) in the specified date according to universal time.
Date.prototype.getUTCSeconds()
    Returns the seconds (0-59) in the specified date according to universal time.
Date.prototype.getYear()
    Returns the year (usually 2-3 digits) in the specified date according to local time. Use getFullYear() instead.

Setter

Date.prototype.setDate()
    根据本地时间为指定的日期对象设置月份中的第几天。
Date.prototype.setFullYear()
    根据本地时间为指定日期对象设置完整年份(四位数年份是四个数字)。
Date.prototype.setHours()
    根据本地时间为指定日期对象设置小时数。
Date.prototype.setMilliseconds()
    根据本地时间为指定日期对象设置毫秒数。
Date.prototype.setMinutes()
    根据本地时间为指定日期对象设置分钟数。
Date.prototype.setMonth()
    根据本地时间为指定日期对象设置月份。
Date.prototype.setSeconds()
    根据本地时间为指定日期对象设置秒数。
Date.prototype.setTime()
    通过指定从 1970-1-1 00:00:00 UTC 开始经过的毫秒数来设置日期对象的时间,对于早于 1970-1-1 00:00:00 UTC的时间可使用负值。
Date.prototype.setUTCDate()
    根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。
Date.prototype.setUTCFullYear()
    根据世界时设置 Date 对象中的年份(四位数字)。
Date.prototype.setUTCHours()
    根据世界时设置 Date 对象中的小时 (0 ~ 23)。
Date.prototype.setUTCMilliseconds()
    根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。
Date.prototype.setUTCMinutes()
    根据世界时设置 Date 对象中的分钟 (0 ~ 59)。
Date.prototype.setUTCMonth()
    根据世界时设置 Date 对象中的月份 (0 ~ 11)。
Date.prototype.setUTCSeconds()
    根据世界时设置 Date 对象中的秒钟 (0 ~ 59)。
Date.prototype.setYear()
    setYear() 方法用于设置年份。请使用 setFullYear() 方法代替。

Conversion getter

Date.prototype.toDateString()
    以人类易读(human-readable)的形式返回该日期对象日期部分的字符串。
Date.prototype.toISOString()
    把一个日期转换为符合 ISO 8601 扩展格式的字符串。
Date.prototype.toJSON()
    使用 toISOString() 返回一个表示该日期的字符串。为了在 JSON.stringify() 方法中使用。
Date.prototype.toGMTString()
    返回一个基于 GMT (UT) 时区的字符串来表示该日期。请使用 toUTCString() 方法代替。
Date.prototype.toLocaleDateString()
    返回一个表示该日期对象日期部分的字符串,该字符串格式与系统设置的地区关联(locality sensitive)。
Date.prototype.toLocaleFormat()
    使用格式字符串将日期转换为字符串。
Date.prototype.toLocaleString()
    返回一个表示该日期对象的字符串,该字符串与系统设置的地区关联(locality sensitive)。覆盖了 Object.prototype.toLocaleString() 方法。
Date.prototype.toLocaleTimeString()
    返回一个表示该日期对象时间部分的字符串,该字符串格式与系统设置的地区关联(locality sensitive)。
Date.prototype.toSource()
    返回一个与Date等价的原始字符串对象,你可以使用这个值去生成一个新的对象。重写了 Object.prototype.toSource() 这个方法。
Date.prototype.toString()
    返回一个表示该日期对象的字符串。覆盖了Object.prototype.toString() 方法。
Date.prototype.toTimeString()
    以人类易读格式返回日期对象时间部分的字符串。
Date.prototype.toUTCString()
    把一个日期对象转换为一个以UTC时区计时的字符串。
Date.prototype.valueOf()
    返回一个日期对象的原始值。覆盖了 Object.prototype.valueOf() 方法。






几个案例:

<script>
    var time = 0;
    time = new Date(1507601410000);//毫秒转日期格式
    console.log("time:"+time);
    console.log("time.toDateString():"+time.toDateString());
    console.log("time.toJSON():"+time.toJSON());
    console.log("time.toUTCString():"+time.toUTCString());
    console.log("time.toLocaleDateString():"+time.toLocaleDateString());
    console.log("time.toLocaleString():"+time.toLocaleString());
    console.log("time.toLocaleTimeString():"+time.toLocaleTimeString());
    console.log("time.valueOf():"+time.valueOf());
</script>

输出结果:

java 实体里的时间字段怎么设置自动生成日期 java怎么设置时间格式_时间格式_02


以上的日期格式函数可以满足我们的部分需求,但是如果我们还是需要特定的日期格式如:2017-10-10 10:10就需要自定义Date的原型函数


自定义js的Date日期转换函数

直接看例子

<script>
    var time = 0;
    time = new Date(1507601410000);//毫秒转日期格式
    console.log("time:"+time);

    //时间转换:毫秒类型转成2017-1-2 12:20
    Date.prototype.toMyDateString = function() {
        return this.getFullYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate() + " " + this.getHours() + ":" + this.getMinutes();
    };

    //调用
    console.log("time.toMyDateString():"+time.toMyDateString());
</script>



输出:


java 实体里的时间字段怎么设置自动生成日期 java怎么设置时间格式_时间格式_03


如图自定义的函数成功使用。那么至此就知道如果要定义自己想过要的转换格式函数,函数的编写格式就是如此:

Date.prototype.函数名 = function() {
        ...转换代码...
        return  结果;
}
//调用直接和调用geitTime()方法一样



一个js通用型时间转换函数

<script>
    var time = 0;
    time = new Date(1507601410256);//毫秒转日期格式
    console.log("time:"+time);

    //使用函数的方式进行定义
    function dateFormat(fmt,date){
        var o = {
            "M+" : date.getMonth()+1,                 //月份
            "d+" : date.getDate(),                    //日
            "h+" : date.getHours(),                   //小时
            "m+" : date.getMinutes(),                 //分
            "s+" : date.getSeconds(),                 //秒
            "q+" : Math.floor((date.getMonth()+3)/3), //季度
            "S"  : date.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt)){
            fmt=fmt.replace(RegExp.$1, (date.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;
    }

    //使用原型方法的方式定义
    Date.prototype.timeFormat = function(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;
    }

    //调用
    console.log("dateFormat():"+dateFormat("MM-dd hh:mm 第q季度",time));
    console.log("dateFormat():"+dateFormat("yyyy-MM-dd hh:mm:s 第q季度",time));
    console.log("dateFormat():"+dateFormat("yyyy年MM月dd日 hh时mm分ss秒S毫秒 第q季度",time));

    console.log("time.timeFormat():"+time.timeFormat("MM-dd hh:mm 第q季度"));
    console.log("time.timeFormat():"+time.timeFormat("yyyy-MM-dd hh:mm:s 第q季度"));
    console.log("time.timeFormat():"+time.timeFormat("yyyy年MM月dd日 hh时mm分ss秒S毫秒 第q季度"));
</script>

以上两种形式,使用方式不同而已,功能一样。注意能够转换的格式只能由函数的var o ={...}定义的形式进行定制格式。参考案例

以上案例的输出结果:

java 实体里的时间字段怎么设置自动生成日期 java怎么设置时间格式_javascript_04


ok至此,以后有关于js的时间日期格式转换问题就可以参考这里了