日期時間幫助類,提供日期時間常用方法封裝

package cn.wtu.broadcast.util;

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 huangjiakui
 * @date 2018年12月04日
 *
 */
public class DateUtil {
	public final static SimpleDateFormat sdFormat1 = new SimpleDateFormat("yyyy-MM-dd");
	public final static SimpleDateFormat sdFormat2 = new SimpleDateFormat("yyyyMMdd");
	public final static SimpleDateFormat sdFormat3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public final static SimpleDateFormat sdFormatSecond = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	private static Logger logger = LoggerFactory.getLogger(DateUtil.class);

	public static String format(Date date, SimpleDateFormat sdf) {
		return sdf.format(date);
	}

	public static String getNow() {
		return sdFormat3.format(new Date());
	}

	public static String getDate(Date date) {
		return sdFormat1.format(date);
	}

	public static String getDateForInt(long i) {
		if (i > 0) {
			return sdFormat3.format(new Date(i * 1000L));
		} else {
			return "";
		}
	}

	public static Date parse(String date) {
		try {
			return sdFormat2.parse(date);
		} catch (ParseException e) {
			logger.error(e.getMessage(),e);
			return null;
		}
	}

	public static Date parse(String date, String format) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.parse(date);
		} catch (ParseException e) {
			logger.error(e.getMessage(),e);
			return null;
		}
	}

	/**
	 * 獲取當前日期是星期幾<br>
	 * 
	 * @param dt
	 * @return 當前日期是星期幾
	 */
	public static int getWeekOfDate(Date dt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);

		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;

		return w == 0 ? 7 : w;
	}

	/**
	 * 獲得 當前 或者參數時間是今年的第幾周
	 * 
	 * @param dt
	 * @return
	 */
	public static int getWeekIndex(Date dt) {
		if (dt == null) {
			dt = new Date();
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		long time = 0;
		try {
			// 今年 1月1號 0點
			time = sdf.parse(sdf.format(dt)).getTime();
		} catch (ParseException e) {
			logger.error(e.getMessage(),e);
		}
		if (time == 0) {
			return -1;
		}
		// 從今年1月1號到現在過去的時間
		long lapsed = System.currentTimeMillis() - time + getWeekOfDate(new Date(time)) * 86400000;

		// 這個星期已經過去的時間
		long remainder = lapsed % (86400000 * 7);

		return (int) ((lapsed - remainder) / (86400000 * 7) + (remainder == 0 ? 0 : 1));
	}

	/**
	 * 獲得 榜單批次號
	 * 
	 * @param time
	 *            如果time爲空 則返回當期批次號 如果是今年第一週 則檢查去年的最後一週
	 * @return
	 */
	public static int getBatchid(long time) {
		// 本週是今年的第幾周
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		int index = DateUtil.getWeekIndex(time == 0 ? null : new Date(time));
		int year = Integer.valueOf(sdf.format(time == 0 ? new Date() : new Date(time)));
		// 如果是第一週 則檢查去年的最後一週
		if (index == 1) {
			index = DateUtil.getWeekIndex(time == 0 ? null : new Date(time - (8640000 * 7)));
			year -= 1;
		}
		return year * 100 + index;
	}

	/**
	 * 
	 * @param date
	 *            開始時間
	 * @param addTime
	 *            增加時間數量
	 * @param unit
	 *            時間單位 時 分 秒 日 月 年 等
	 * @return
	 */

	public static Date addTime(Date date, int addTime, int unit) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(unit, addTime);
		return cal.getTime();
	}

	/**
	 * 獲取時間date1與date2相差的秒數
	 * 
	 * @param date1
	 *            起始時間
	 * @param date2
	 *            結束時間
	 * @return 返回相差的秒數
	 */
	public static int getOffsetSeconds(Date date1, Date date2) {
		int seconds = (int) ((date2.getTime() - date1.getTime()) / 1000);
		return seconds;
	}

	/**
	 * 獲取時間date1與date2相差的分鐘數
	 * 
	 * @param date1
	 *            起始時間
	 * @param date2
	 *            結束時間
	 * @return 返回相差的分鐘數
	 */
	public static int getOffsetMinutes(Date date1, Date date2) {
		return getOffsetSeconds(date1, date2) / 60;
	}

	/**
	 * 獲取時間date1與date2相差的小時數
	 * 
	 * @param date1
	 *            起始時間
	 * @param date2
	 *            結束時間
	 * @return 返回相差的小時數
	 */
	public static int getOffsetHours(Date date1, Date date2) {
		return getOffsetMinutes(date1, date2) / 60;
	}

	/**
	 * 獲取時間date1與date2相差的天數數
	 * 
	 * @param date1
	 *            起始時間
	 * @param date2
	 *            結束時間
	 * @return 返回相差的天數
	 */
	public static int getOffsetDays(Date date1, Date date2) {
		return getOffsetHours(date1, date2) / 24;
	}

	/**
	 * 獲取時間date1與date2相差的週數
	 * 
	 * @param date1
	 *            起始時間
	 * @param date2
	 *            結束時間
	 * @return 返回相差的週數
	 */
	public static int getOffsetWeeks(Date date1, Date date2) {
		return getOffsetDays(date1, date2) / 7;
	}

}

 

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