时间格式处理函数大全

import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pub.toolkit.DateAbout;

/**
 * @category formate 格式为 yyyy-MM-dd
 * @category 转化后的格式为 yyyyMMdd
 * @author Administrator
 * 
 */
public class DataFormat {
   /**
    * @author lzk
    * @param dateStr
    * @param formate
    * @return
    */
	public static final String DATE_TIME_FORMATE = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_FORMATE      = "yyyy-MM-dd"         ;
	public static final String DATE_TIME_FORMATE1 = "yyyy-M-dd HH:mm:ss";
	  
	public static String formateDateToStr(String dateStr, String formate) {
		String newDateStr = "";
		try {
			// 格式化日期
			SimpleDateFormat df = new SimpleDateFormat(formate);
			Date date = df.parse(dateStr.trim());
			newDateStr = df.format(date).replaceAll("-", "");
		} catch (Exception E) {
			 
		}
		return newDateStr;
	}

	public static String formateStr(String dateStr, String formate) {
		String newDateStr = "";
		String formatString = "yyyyMMdd";
		try {
			// 格式化日期
			SimpleDateFormat df = new SimpleDateFormat(formatString);
			Date date = df.parse(dateStr);
			SimpleDateFormat formatDay = new SimpleDateFormat(formate);

			newDateStr = formatDay.format(date);
		} catch (Exception E) {
			 
		}
		return newDateStr;
	}

	public static String formateStr2(String dateStr, String formate) {
		String newDateStr = "";
		String formatString = "yyyyMM";
		try {
			// 格式化日期
			SimpleDateFormat df = new SimpleDateFormat(formatString);
			Date date = df.parse(dateStr);
			SimpleDateFormat formatDay = new SimpleDateFormat(formate);

			newDateStr = formatDay.format(date);
		} catch (Exception E) {
			 
		}
		return newDateStr;
	}
   
	public static String formateStr16(String dateStr, String formate) {
		String newDateStr = "";
		String formatString = "yyyyMMddHHmmss";
		try {
			// 格式化日期
			SimpleDateFormat df = new SimpleDateFormat(formatString);
			Date date = df.parse(dateStr);
			SimpleDateFormat formatDay = new SimpleDateFormat(formate);

			newDateStr = formatDay.format(date);
		} catch (Exception E) {
			 
		}
		return newDateStr;
	}
	 /**
	  * @author lzk
	  * @return
	  */
	 public static String formateDateToStr(){
		 Date nowTime=new Date();
		 SimpleDateFormat time=new SimpleDateFormat(DATE_TIME_FORMATE);
		 return time.format(nowTime) ;
	 }
	 
	 public static String formateDateToStr2(){
		 Date nowTime=new Date();
		 SimpleDateFormat time=new SimpleDateFormat(DATE_TIME_FORMATE1);
		 return time.format(nowTime) ;
	 }
	 
	 /**
	  * 
	  * @param 2010-04-14 19:13:50
	  * @param yyyyMMddHHmmss
	  * @@author lzk
	  */
	  public static synchronized  String formateDateToNeedStr(String formateString ){
		  
		  if(formateString ==null||"".equals(formateString)){
			  return formateString;
		  }
		    
		  formateString  = formateString.replaceAll(":", "-");
		  formateString  = formateString.replaceAll(" ", "-");
		  formateString  = formateString.replaceAll("-", "");
		  return formateString;
	  }
	 /**
	  * 
	  * @param 20101010101112
	  * @return   2010-10-10 10:11:12
	  * @author lzk
	  */
	 public static synchronized String formateDateToStr(String formateStr){
		 String year  = "";
		 String month = "";
		 String day   = "";
		 String hh    = "";
		 String mm    = "";
		 String ss    = "";
		 String newStr = "";
		 int len      = 8;
		 try{
		 year  = formateStr.substring(0,4);       //yyyy
		 month = formateStr.substring(4,6);       //MM
		 day   = formateStr.substring(6,8);       //dd
		 len   = formateStr.trim().length();
		 if(len==8){
			  newStr = year+"-"+month+"-"+day ;
			  return newStr;
		 }
		 hh    = formateStr.substring(8,10);
		 mm    = formateStr.substring(10,12);
		 ss    = formateStr.substring(12,14);
		 newStr = year+"-"+month+"-"+day+" "+hh+":"+mm+":"+ss ;
		 }catch(Exception E){
		 }
		 return newStr;
	 }
	 //格式化系统日期 按照传输格式
	 /**
	  * @param yyyyMMdd 。。。
	  * @author lzk
	  */
	 public static synchronized String formateSystemDateToStr(String formate){
	     SimpleDateFormat df = new SimpleDateFormat(formate);
		 return df.format(new Date()); 
	 }
	 //获取当前月第一天日期
	 public static String  getCurrentMonthFirstDate(){
		DateFormat    df=new    SimpleDateFormat("yyyy-MM-dd");    
 	    Calendar    calendar=Calendar.getInstance();    
 	    Calendar    cpcalendar=(Calendar)calendar.clone();    
 	    cpcalendar.set(Calendar.DAY_OF_MONTH,    1);    
 	    return df.format(    new    Date(cpcalendar.getTimeInMillis()));
	 }
	 //获取当前月当前日期
	 public static String getCurrentDate(){
		 DateFormat    df=new    SimpleDateFormat("yyyy-MM-dd");    
 	     Calendar    calendar=Calendar.getInstance();    
 	     Calendar    cpcalendar=(Calendar)calendar.clone();    
		 cpcalendar.add(Calendar.MONTH,    1);    
 	     cpcalendar.add(Calendar.DATE,    -1);    		 
		 return df.format(    new    Date());
	 }
	 //获取当前月最后日期
	 public static String getLastCurrentMonthDate(){
		 DateFormat    df=new    SimpleDateFormat("yyyy-MM-dd");    
 	     Calendar    calendar=Calendar.getInstance();    
 	     calendar.set(Calendar.DATE, 1);
 	     calendar.roll(Calendar.DATE, -1);
		 return df.format(calendar.getTime());
	 }
	 //获取当前日期是周几
	 public static String getWeekOfYear(String date) {
		 Calendar c = Calendar.getInstance();   
		 c.setTime (strToDate(date));
		 return new SimpleDateFormat("EEEE").format(c.getTime());   
	}
	 //日期字段串对象转换
	  public static Date strToDate(String strDate) {
	         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");   
	         ParsePosition pos = new ParsePosition(0);   
	         Date strtodate = formatter.parse(strDate, pos);   
	        return strtodate;   
	 }
	  
	 //获取当月周范围
	  public static Map getWeekRange(){
		  Calendar   calendar   =   Calendar.getInstance(); 
		  calendar.setTime(new   Date()); 
		  int   days  =  calendar.getMaximum(Calendar.DATE);
		  Map   DateMap                  =  new HashMap()       ;//周集合
		  int   weekIndex                =  1 ;
		  int   index                    =  1 ;
		  String sDate =  "";   //起始日期
		  for(int i=2;i<=days;i++){
			  List  dateList                 =  new ArrayList()     ;//期范围   
			  sDate                          =  getCurrentMonthFirstDate(index);
			  String compareDate             =  getCurrentMonthFirstDate(i);
			  if(getWeekOfYear(compareDate).indexOf("日")>-1){
				  index = i+1 ;
				  sDate = sDate.replaceAll("-", "/");
				  compareDate = compareDate.replaceAll("-", "/");
				  dateList.add(sDate);
				  dateList.add(compareDate);
				  DateMap.put(weekIndex+"", dateList); 
				  weekIndex++;
			  }
			 
		  }
		  return DateMap;
	  }
	//获取当前月第一天日期
		 public static String  getCurrentMonthFirstDate(int nextDay){
			DateFormat    df=new    SimpleDateFormat("yyyy-MM-dd");    
	 	    Calendar    calendar=Calendar.getInstance();    
	 	    Calendar    cpcalendar=(Calendar)calendar.clone();    
	 	    cpcalendar.set(Calendar.DAY_OF_MONTH,    nextDay);    
	 	    return df.format(    new    Date(cpcalendar.getTimeInMillis()));
		 }
		 
		 public static String formateDate(Date date, String formate) {
				String newDateStr = "";
				SimpleDateFormat df = new SimpleDateFormat(formate);
				newDateStr = df.format(date);
				return newDateStr;
			}
		 
     public static String getCurDate(){
    	 
    	 DateFormat    df=new    SimpleDateFormat("yyyy-MM");    
 	     Calendar    calendar=Calendar.getInstance();    
 	     Calendar    cpcalendar=(Calendar)calendar.clone();    
		 cpcalendar.add(Calendar.MONTH,    1);    
 	     cpcalendar.add(Calendar.DATE,    -1);    		 
		 return df.format(    new    Date());
    	 
     } 		 
     
     public static String getCurDateToYYYYMMDD(){
       	 
       	 DateFormat    df=new    SimpleDateFormat("yyyyMMDD");    
    	     Calendar    calendar=Calendar.getInstance();    
    	     Calendar    cpcalendar=(Calendar)calendar.clone();    
    		 cpcalendar.add(Calendar.MONTH,    1);    
    	     cpcalendar.add(Calendar.DATE,    -1);    		 
    		 return df.format(    new    Date());
       	 
        } 
        
     
     //获取系统当前时间
     /**
      * formatter:yyyyMMddHHmm
      */
     public static String getCurrentDateToNYR(){
    	 java.util.Date date2 = new java.util.Date();
		 SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
		 String str2 = formatter.format(date2);
		 return str2 ;
     }
     
   //获取系统当前时间
     /**
      * formatter:yyyyMMddHHmm
      */
     public static String getCurrentDateToScends(){
    	 java.util.Date date2 = new java.util.Date();
		 SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
		 String str2 = formatter.format(date2);
		 return str2 ;
     }
     
   //获取系统当前时间精确到秒
     /**
      * formatter:yyyyMMddHHmmss
      */
     public static String getCurrentDateToSeconds(){
    	 java.util.Date date2 = new java.util.Date();
		 SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		 String str2 = formatter.format(date2);
		 return str2 ;
     }
     
     /**
      * formatter:yyyyMMddHHmmssSSS
      */
     public static String getCurrentDateToMilSeconds(){
    	 java.util.Date date2 = new java.util.Date();
		 SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		 String str2 = formatter.format(date2);
		 return str2 ;
     }
     
     /**
      * @value  yyyy-mm-dd
      * @param formateDate yyyyMMddHHmmss 
      * @return
      */
    public  static String formateDateToYMD(String formateDate){
    	String year = formateDate.substring(0,4);
    	String month = formateDate.substring(4,6);
    	String day   = formateDate.substring(6,8);
    	
    	return year+"-"+month+"-"+day ;
    	
    } 
    /**
     * 取得20天之前的日期
     * @return
     */
   public static  String getPre20DaysDateStr() {
        Date tmpDate=new Date();
        tmpDate=DateAbout.addDate(Calendar.DATE,-20,tmpDate);
        String pre20DaysDateStr = DateAbout.getDateTimeStrByFormat(tmpDate,"yyyy-MM-dd");    
        return pre20DaysDateStr;
   }
   /**
    * 取得当前的日期 格式是yyyy-MM-dd
    * @return
    */
   public static String getCurrentDateStr(){
	    String cDateStr=DateAbout.getCurreentDateByFormat("yyyy-MM-dd");
	    return cDateStr;
	   
   }
     
   /**
    * 取得20天之后的日期
    * @return
    */
   public  static String getNext20DaysDateStr() {
     Date tmpDate=new Date();
     tmpDate=DateAbout.addDate(Calendar.DATE,20,tmpDate);
     String  next20DaysDateStr = DateAbout.getDateTimeStrByFormat(tmpDate,"yyyy-MM-dd");   
     return next20DaysDateStr;
   }
   
	 public static void main(String[] args){
		 //java.util.Date date2 = new java.util.Date();
		 //SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
		 //String str2 = formatter.format(date2);
		 
		 System.out.println("当前日期:"+formateDateToStr("20120210153316"));
		 
		 
		 //System.out.println(formateDateToStr("2010-01-02","yyyy-MM-dd"));
		// System.out.println("开始日期:"+getCurrentMonthFirstDate());
		// System.out.println("结束日期:"+getLastCurrentMonthDate());
		 //System.out.println(getWeekOfYear("2010-06-30"));
		 //System.out.println(getWeekRange());
		// System.out.println(formateDateToStr()); 
		 
	 }
}

你可能感兴趣的:(java编程)