- var mydate = new Date('2012-05-10 10:10:21');
- console.log("格式化事件格式1--" + mydate.getTime());//输出的是:格式化事件格式1--1336615821000
- 扩展Date对象的属性
- Date.prototype.format = function (format) {
- var o = {
- "M+": this.getMonth() + 1, //month
- "d+": this.getDate(), //day
- "h+": this.getHours(), //hour
- "m+": this.getMinutes(), //minute
- "s+": this.getSeconds(), //second
- "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
- "S": this.getMilliseconds() //millisecond
- }
- if (/(y+)/.test(format))
- format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
- for (var k in o)
- if (new RegExp("(" + k + ")").test(format))
- format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
- return format;
- }
- console.log("格式化事件格式2--" + (new Date(mydate.getTime())).format("yyyy-MM-dd hh:mm:ss"));//输出的是:格式化事件格式2--2012-05-10 10:10:21
- Date d = new Date();
- String s;
- /** Date类的格式: Sat Apr 16 13:17:29 CST 2006 */
- System.out.println(d);
- System.out.println("******************************************");
- /** getDateInstance() */
- /** 输出格式: 2006-4-16 */
- s = DateFormat.getDateInstance().format(d);
- System.out.println(s);
- /** 输出格式: 2006-4-16 */
- s = DateFormat.getDateInstance(DateFormat.DEFAULT).format(d);
- System.out.println(s);
- /** 输出格式: 2006年4月16日 星期六 */
- s = DateFormat.getDateInstance(DateFormat.FULL).format(d);
- System.out.println(s);
- /** 输出格式: 2006-4-16 */
- s = DateFormat.getDateInstance(DateFormat.MEDIUM).format(d);
- System.out.println(s);
- /** 输出格式: 06-4-16 */
- s = DateFormat.getDateInstance(DateFormat.SHORT).format(d);
- System.out.println(s);
- /** 输出格式: 2006-01-01 00:00:00 */
- java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- s = format1.format(new Date());
- System.out.println(s);
- /** 输出格式: 2006-01-01 01:00:00 */
- System.out.println((new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(new Date()));
- /** 输出格式: 2006-01-01 13:00:00 */
- System.out.println((new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
- /** 输出格式: 20060101000000***/
- java.text.DateFormat format2 = new java.text.SimpleDateFormat("yyyyMMddhhmmss");
- s = format2.format(new Date());
- System.out.println(s);
- /**取得上一个月的月份**/
- public static Date getLastMonth(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.add(Calendar.MONTH, -1);
- return cal.getTime();
- }