日期操作工具類

package com.iss.gtreasury.financing.common.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @ClassName: DateUtil
 * @Description:日期操作工具類
 * @author nagang
 * 創建時間: 2019年3月7日 上午11:29:30
 */
public class DateUtil {
	private static Logger logger = LoggerFactory.getLogger(DateUtil.class);
	/**
	 * @Title: getCurrentTimestamp
	 * @Description: 獲取Timestamp類型的當前時間
	 * @return
	 * @author nagang
	 * 創建時間: 2019年3月7日 上午11:29:43
	 */
	public static Timestamp getCurrentTimestamp(){
		return new Timestamp(System.currentTimeMillis());
	}

	/**
	 * @Title: dateAdd
	 * @Description: 對日期添加天數
	 * @param date
	 * @param increament
	 * @return
	 * @author nagang
	 * 創建時間: 2019年3月7日 上午11:30:09
	 */
	public static Date dateAdd(Date date,int increament){
		Calendar c = Calendar.getInstance();
	    c.setTime(date);
	    c.add(Calendar.DAY_OF_MONTH, increament);
	    return c.getTime();
	}

	/**
	 * @Title: DateToString
	 * @Description:格式換時間爲字符串 yyyy-MM-dd
	 * @param date
	 * @return
	 * @author lp
	 * 創建時間: 2019年4月13日 上午11:40:05
	 */
	public static String DateToString(Date date) {
		String strDate = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (date != null) {
			strDate = sdf.format(date);
		}
		return strDate;
	}
	public static Date StringToDate(String dateString) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (dateString != null) {
			try {
				date = sdf.parse(dateString);
			} catch (ParseException e) {
				logger.error(e.getMessage());
			}
		}
		return date;  
	}
	/**
	 * 獲取當前年
	 */
	public static class NowYear {
		public static final int getNowYear() {
			Calendar calendar = Calendar.getInstance();
			int year = calendar.get(Calendar.YEAR);
			return year;
		}
		public static final int getNowMonth() {
			Calendar calendar = Calendar.getInstance();
			int month = calendar.get(Calendar.MONTH);
			return month+1;
		}

		  /**
	     *
	     * <獲取當前自然日及前後N年>
	     * @param n
	     * @see [Classes, Class#method, Class#members]
	     */
	    public static Integer[] getFromToYearByNowYear(int n) {
	        if(n>=0) {
	            int nowYear = NowYear.getNowYear();
	            int length=n*2+1;
	            int sub=n;
	            int add=n;
	            Integer[] years=new Integer[length];
	            for (int i = 0; i < years.length; i++)
	            {
	                if(i<n) {
	                    years[i]=nowYear+add;
	                    add--;
	                }
	                if(i==n) {
	                    years[i]=nowYear;
	                }
	                if(i>n) {
	                    years[i]=nowYear-sub;
	                    sub--;
	                }
	            }
	            return years;
	        }
	        return null;
	    }
	}
	
	/**
     * @Title: formatHourMinSecond
     * @Description:格式化時間時分秒
     * @param date
     * @return
     * @author lp
     * 創建時間: 2019年4月13日 上午11:40:05
     */
    public static Date formatHourMinSecond(Date date, int hour, int min, int second) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, min);
        cal.set(Calendar.SECOND, second);
        //cal.set(Calendar.MILLISECOND, 999);
        
        return cal.getTime();
    }

    /**
     * 計算兩個日期的時間差
     * @param beginDate 開始日期
     * @param endDate 結束日期
     * @return
     * @throws ParseException
     */
    public static Long daysBetween(Date beginDate,Date endDate) throws ParseException {
	    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	    beginDate=sdf.parse(sdf.format(beginDate));
	    endDate=sdf.parse(sdf.format(endDate));
	    Calendar cal = Calendar.getInstance();
	    cal.setTime(beginDate);
	    long time1 = cal.getTimeInMillis();
	    cal.setTime(endDate);
	    long time2 = cal.getTimeInMillis();
	    long betwiinDays=(time2-time1)/(1000*3600*24);
	    return betwiinDays;
    }
    
    public static void main(String[] args) throws ParseException {
    	Date nowDay = new Date();
    	Date endDay = dateAdd(nowDay, 3);
    	System.out.println(daysBetween(nowDay , endDay));
    }
    	
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章