JS 和 Java Date时间格式的转换

13 篇文章 0 订阅
[javascript]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. var mydate = new Date('2012-05-10 10:10:21');  
  2.     console.log("格式化事件格式1--" + mydate.getTime());//输出的是:格式化事件格式1--1336615821000  
  3. 扩展Date对象的属性  
  4. Date.prototype.format = function (format) {  
  5.         var o = {  
  6.             "M+"this.getMonth() + 1, //month  
  7.             "d+"this.getDate(), //day  
  8.             "h+"this.getHours(), //hour  
  9.             "m+"this.getMinutes(), //minute  
  10.             "s+"this.getSeconds(), //second  
  11.             "q+": Math.floor((this.getMonth() + 3) / 3), //quarter  
  12.             "S"this.getMilliseconds() //millisecond  
  13.         }  
  14.         if (/(y+)/.test(format))  
  15.             format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
  16.         for (var k in o)  
  17.             if (new RegExp("(" + k + ")").test(format))  
  18.                 format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));  
  19.         return format;  
  20.     }  
  21. console.log("格式化事件格式2--" + (new Date(mydate.getTime())).format("yyyy-MM-dd hh:mm:ss"));//输出的是:格式化事件格式2--2012-05-10 10:10:21  

Java代码   收藏代码
  1. Date d = new Date();  
  2.         String s;  
  3.            
  4.         /** Date类的格式: Sat Apr 16 13:17:29 CST 2006 */  
  5.         System.out.println(d);  
  6.            
  7.         System.out.println("******************************************");    
  8.          
  9.         /** getDateInstance() */  
  10.         /** 输出格式: 2006-4-16 */  
  11.         s = DateFormat.getDateInstance().format(d);  
  12.         System.out.println(s);  
  13.          
  14.         /** 输出格式: 2006-4-16 */  
  15.         s = DateFormat.getDateInstance(DateFormat.DEFAULT).format(d);  
  16.         System.out.println(s);  
  17.          
  18.         /** 输出格式: 2006年4月16日 星期六 */  
  19.         s = DateFormat.getDateInstance(DateFormat.FULL).format(d);  
  20.         System.out.println(s);  
  21.          
  22.         /** 输出格式: 2006-4-16 */  
  23.         s = DateFormat.getDateInstance(DateFormat.MEDIUM).format(d);  
  24.         System.out.println(s);  
  25.          
  26.         /** 输出格式: 06-4-16 */  
  27.         s = DateFormat.getDateInstance(DateFormat.SHORT).format(d);  
  28.         System.out.println(s);  
  29.          
  30.         /** 输出格式: 2006-01-01 00:00:00 */  
  31.         java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
  32.         s = format1.format(new Date());  
  33.         System.out.println(s);  
  34.          
  35.         /** 输出格式: 2006-01-01 01:00:00 */  
  36.         System.out.println((new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(new Date()));  
  37.          
  38.   
  39.         /** 输出格式: 2006-01-01 13:00:00 */  
  40.         System.out.println((new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));  
  41.   
  42.         /** 输出格式: 20060101000000***/  
  43.         java.text.DateFormat format2 = new java.text.SimpleDateFormat("yyyyMMddhhmmss");  
  44.         s = format2.format(new Date());  
  45.         System.out.println(s);   
  46.    /**取得上一个月的月份**/  
  47. public static Date getLastMonth(Date date) {  
  48.         Calendar cal = Calendar.getInstance();  
  49.         cal.setTime(date);  
  50.         cal.add(Calendar.MONTH, -1);  
  51.         return cal.getTime();  
  52.     }  

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值