자바 날짜 형식 변환
public class DateFormatUtils {
	private static Log logger = LogFactory.getLog(DateFormatUtils.class);
	
	public static String formatDate(String formater,Date date){
		SimpleDateFormat formate = new SimpleDateFormat(formater);
		formate.format(date);
		return formate.format(date);
	}
	
	/**
	 * 
	 * @Title:formatDateToCommon
	 * @Description:          
	 * @param date
	 * @return
	 */
	public static String formatDateToCommon(Date date){
		SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formate.format(date);
	}
	
	/**
	 * 
	 * @Title:getSystemDate
	 * @Description:          
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date getSystemDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return sdf.parse(sdf.format(new Date()));
		} catch (ParseException e) {
			logger.error("", e);
		}
		return null ;
	}
	
	/**
	 * 
	 * @Title:SystemDateFormatToCommon
	 * @Description:          
	 * @return
	 */
	public static String getSystemDateFormatToCommon(){
		SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formate.format(new Date());
	}
	
	public static String getSystemDateFormatToYYYYMMDD(){
		SimpleDateFormat formate = new SimpleDateFormat("yyyyMMdd");
		return formate.format(new Date());
	}
	
	public static String getSystemDateFormatToYYYYMMDDHHmmss(){
		SimpleDateFormat formate = new SimpleDateFormat("yyyyMMddHHmmss");
		return formate.format(new Date());
	}
	
	/**
	 * 
	 * @Title:getFormatDateCommon
	 * @Description:       
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date getFormatDateCommon(Date date) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return sdf.parse(sdf.format(date));
		} catch (ParseException e) {
			logger.error("", e);
		}
		return null;
	}
	
	/**
	 * 
	 * @Title:StringToDate
	 * @Description:          
	 * @param dateStr
	 * @param formatStr
	 * @return
	 * @throws ParseException 
	 */
	public static Date StringToDate(String dateStr) throws ParseException {
		DateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=null;
		date = sdf.parse(dateStr);
		return date;
	}
	
	public static Date StringToDate(String dateStr, String pattern){
		try{
			DateFormat sdf=new SimpleDateFormat(pattern);
			Date date = sdf.parse(dateStr);
			return date;
		}catch(ParseException ex){
			return null;
		}
	}
	
	/**
	 * 
	 * @Title:fromDateStringToLong
	 * @Description:               
	 * @param inVal
	 * @return
	 */
	public static long fromDateStringToLong(String inVal) {
		return fromDateStringToLong(inVal, "yyyy-MM-dd HH:mm:ss");
	}
	public static long fromDateStringToLong(String inVal,String format) {
		Date date = null; //       
		SimpleDateFormat inputFormat = new SimpleDateFormat(format);
		try {
			date = inputFormat.parse(inVal); //           
		} catch (Exception e) {
			logger.error("", e);
		}
		return date.getTime(); //      
	}
	
	/**
	 * 
	 * @Title:getMillForDateTimeDouble
	 * @Description:              
	 * @param inVal
	 * @return
	 */
	public static long getMillForDateTimeDouble(Date startTime, Date endTime) {
		long lTime = startTime.getTime();
		long eTime = endTime.getTime();
		long s = eTime - lTime ;
		return s;
	}
	
	/**
	 * 
	 * @Title:formatDuring
	 * @Description:            
	 * @param mss
	 * @return
	 */
	public static String formatDuring(long mss) {  
	    long days = mss / (1000 * 60 * 60 * 24);  
	    long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);  
	    long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);  
	    long seconds = (mss % (1000 * 60)) / 1000;  
    	return days + "   " + hours + "   " + minutes + "   "  
	            + seconds + "   ";   
	}  
	
	/**  
     *                
     * @param format yyyyMMdd
     * @param smdate       
     * @param bdate        
     * @return      
     * @throws ParseException  
     */    
    public static int daysBetween(String format,Date smdate,Date bdate) throws ParseException    
    {    
        SimpleDateFormat sdf=new SimpleDateFormat(format);  
        smdate=sdf.parse(sdf.format(smdate));  
        bdate=sdf.parse(sdf.format(bdate));  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(smdate);    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(bdate);    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  
            
       return Integer.parseInt(String.valueOf(between_days));           
    }    
      
	/**  
     *                
     *            
     * @param format yyyyMMdd
     * @param smdate       
     * @param bdate        
     * @return      
     * @throws ParseException  
     */
    public static int daysBetween(String format,String smdate,String bdate) throws ParseException{  
        SimpleDateFormat sdf=new SimpleDateFormat(format);  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(sdf.parse(smdate));    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(sdf.parse(bdate));    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  
            
       return Integer.parseInt(String.valueOf(between_days));     
    }
    
    /**
     * 
     * @Title:getSystemAddMinute
     * @Description:             + n       
     * @param currentTimeMillis          
     * @param minute  
     * @return
     */
    public static String getSystemByCurrentTimeMillisAndMinute(long currentTimeMillis, int minute){
    	long currentTime = System.currentTimeMillis() + minute * 60 * 1000;
    	Date date = new Date(currentTime);
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
    }
    
    /**
     * 
     * @Title:getDateStrByTimeMillis
     * @Description:                 
     * @param currentTimeMillis
     * @return
     */
    public static String getDateStrByTimeMillis(long currentTimeMillis){
    	Date date = new Date(currentTimeMillis); 
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
    }
    
    /**
     * 
     * @Title:getDateStrByTimeMillis
     * @Description:                 
     * @param currentTimeMillis
     * @return
     */
    public static Date getDateByTimeMillis(long currentTimeMillis){
    	Date date = new Date(currentTimeMillis); 
    	return date;
    }
    
    /**** 
     *        ,          。 
     *  
     * @param date 
     *              (2014-04-20) 
     * @return 2014-03-20 
     * @throws ParseException 
     */  
    public static String addMonth(String yearMonth) throws ParseException {  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");  
        Date dt = sdf.parse(yearMonth);  
        Calendar rightNow = Calendar.getInstance();  
        rightNow.setTime(dt);  
  
        rightNow.add(Calendar.MONTH, +1);  
        Date dt1 = rightNow.getTime();  
        String reStr = sdf.format(dt1);  
  
        return reStr;  
    }
    
    /**
     * 
     * @param dateStr           
     * @param formater                  
     * @param monthCount         ,        
     * @return
     * @throws ParseException 
     */
    public static String calMonth(String dateStr, String formater, int monthCount) throws ParseException{
    	SimpleDateFormat sdf = new SimpleDateFormat(formater);
        Date dt = sdf.parse(dateStr);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
  
        rightNow.add(Calendar.MONTH, monthCount);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.