自定義一個 Date 處理函數

package com.ht.core.web;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 
 * @author: SHF
 * @date: 2017年11月21日 上午10:38:01
 * @Description: 返回一個時間戳  
 * 自定一個日期處理類 
 * return: 返回當前日期加時間的STRING 格式:20170101202334
 */
public class DateTimeTest {

	public static void main(String[] args) {
		System.out.println(getDateTime());
	}
	
	
	public static String getDateTime(){
		SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date date = new java.util.Date();
		String strDate = sFormat.format(date);
		
		System.out.println(strDate);
		String newDateStr=strDate.substring(0, 4)+strDate.substring(5,7)+strDate.substring(8,10)
			+strDate.substring(11, 13)+strDate.substring(14, 16)+strDate.substring(17,19);
		return newDateStr;
	}

}
package com.ht.core.web;

import java.util.Calendar;
import java.util.Date;
/**
 * 
 * @author: SHF
 * @date: 2017年11月22日 上午10:19:28
 * @Description:根據當前輸入的日期來獲取當前的星期幾
 */
public class GetDayTest {

	public static void main(String[] args) {
		
		System.out.println(getTodayWeekday(new Date()));
	}
	
	/**
	 * 根基你輸入的日期  返回星期幾
	 * @return
	 */
	public  static  String getTodayWeekday(Date date){
		
		String weekday=null;
		Calendar cal=Calendar.getInstance();
		cal.setTime(date);
		int intWeek = cal.get(Calendar.DAY_OF_WEEK);
		
		switch(intWeek){
			case 1: 
				weekday="星期日";
				break;
			case 2: 
				weekday="星期一";
				break;
			case 3: 
				weekday="星期二";
				break;
			case 4: 
				weekday="星期三";
				break;
			case 5: 
				weekday="星期四";
				break;
			case 6: 
				weekday="星期五";
				break;
			case 7: 
				weekday="星期六";
				break;
			default:
				weekday="null";
				break;
		}
		
		return weekday;
	}

}

js 中日期的處理

//設置日期數據
var  date1="${parms.equipValidtime1 }";
var  date2="${parms.equipValidtime2 }";
var oldTime1 = (new Date(date1)).getTime();
 var curTime1 = new Date(oldTime1).format("yyyy-MM-dd");
console.log(curTime1);
var oldTime2 = (new Date(date2)).getTime();
var curTime2 = new Date(oldTime2).format("yyyy-MM-dd");
 console.log(curTime2);


Date.prototype.format = 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; 
   }
相關文章
相關標籤/搜索