DateFormat.java 6.6 KB
package com.framework.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间 日期 与字符串 转换处理工具类
 * @author s
 *
 */
public class DateFormat {
	
	/**
	 * 获取当前年月日字符串
	 * 20130708
	 * @return
	 */
	public static String getYmd(){
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH)+1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		
		return ""+year+(month <10?"0"+month:month)+(day <10?"0"+day:day);
	}
	
	/**
	 * 把字符串转换成Date
	 * @param date
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public static Date formatDate(String dateSource,String format){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = simpleDateFormat.parse(dateSource);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * 把字Date换成Date
	 * @param date
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public static Date formatDate(Date dateSource,String format){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = simpleDateFormat.parse(formatString(dateSource,format));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	
	
	/**
	 * 把日期转换成特定字符串
	 * @param dateSource
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public static String formatString(Date dateSource,String format){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		String str = null;
		str = simpleDateFormat.format(dateSource);
		return str;
	}
	
	/**
	 * 把日期转换成特定字符串
	 * @param dateSource
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public static String formatString(String dateSource,String format){
		Date d = formatDate(dateSource,format);
		String str = null;
		str = formatString(d,format);
		return str;
	}
	
	/**
	 * 查询上一个月时间
	 * @return
	 */
	public static Date getBeforeMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -1);
		return formatDate(calendar.getTime(),"yyyy-MM");
	}
	
	/**
	 * 查询上下个月时间
	 * @return
	 */
	public static Date getAfterMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, 1);
		return formatDate(calendar.getTime(),"yyyy-MM");
	}
	
	/**
	 * 查询上下个月时间
	 * @return
	 */
	public static Date getAfterMonth(Date targetDate) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(targetDate);
		calendar.add(Calendar.MONTH, 1);
		return formatDate(calendar.getTime(),"yyyy-MM");
	}
	
	/**
	 * 查询月
	 * @return
	 */
	public static Date getMonth(Date targetDate) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(targetDate);
		return formatDate(calendar.getTime(),"yyyy-MM");
	}
	
	/**
	 * 添加 n days 自然日
	 * @param source
	 * @param days
	 * @return
	 */
	public static Date addDays(Date source,int days) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(source);
		calendar.add(Calendar.DATE, days);
		return calendar.getTime();
	}
	
	
	/**
	 * 添加 n days 工作日
	 * @param source
	 * @param days
	 * @return
	 */
	public static Date addWorkDays(Date source,int days) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(source);
		//循环
		for(int i=1;i<=days;i++) {
			calendar.add(Calendar.DATE, 1);
			//如果是周六
			if(calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){
				calendar.add(Calendar.DATE, 2);
		    }else if(calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
		    	calendar.add(Calendar.DATE, 1);
		    }
		}
		return calendar.getTime();
	}
	
	/**
	 * 计算两个日期之间相差的月份
	 * @param minDate
	 * @param maxDate
	 * @return
	 */
	public static String[] getMonthBetween(Date minDate, Date maxDate){
		try{
			ArrayList<String> result = new ArrayList<String>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
			Calendar min = Calendar.getInstance();
			Calendar max = Calendar.getInstance();
			min.setTime(sdf.parse(formatString(minDate, "yyyy-MM")));
			min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
			max.setTime(sdf.parse(formatString(maxDate, "yyyy-MM")));
			max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
			Calendar curr = min;
			while (curr.before(max)) {
			 result.add(sdf.format(curr.getTime()));
			 curr.add(Calendar.MONTH, 1);
			}
			String[] months = new String[result.size()+1];
			for(int i=0;i<result.size();i++) {
				months[i] = result.get(i);
			}
			months[result.size()] = formatString(minDate, "yyyy");
			return months;
		}catch(ParseException pe) {
			pe.printStackTrace();
		}
		return new String[0];
	}
	
	/**  
     * 计算两个日期之间相差的天数  
     * @param smdate 较小的时间 
     * @param bdate  较大的时间 
     * @return 相差天数 
     * @throws ParseException  
     */    
    public static int daysBetween(Date smdate,Date bdate) {    
    	try{
    		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
            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)); 
    	}catch(Exception e) {
    		e.printStackTrace();
    	}
        return -1;
    }    
      
	/** 
	*字符串的日期格式的计算 
	*/  
    public static int daysBetween(String smdate,String bdate){
    	try{
    		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
            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)); 
    	}catch(ParseException e) {
    		e.printStackTrace();
    	}
        return -1;
    }  
  
	
	
	
	public static void main(String[] args){
		Date d = DateFormat.formatDate("2016-12-29", "yyyy-MM-dd");
		System.out.print(DateFormat.formatString(DateFormat.addWorkDays(d, 7), "yyyy-MM-dd"));
	}

}