1. 程式人生 > >時間工具類(DateUtils)

時間工具類(DateUtils)

在jeesite的基礎上封裝了DateUtils工具類

有獲取年限,判斷是不是週末等等工具

package com.thinkgem.jeesite.common.utils;

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

import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 日期工具類, 繼承org.apache.commons.lang.time.DateUtils類
 * @author xl
 *
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
	
	
	private static String[] parsePatterns = {
		"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
		"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
		"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

	/**
	 * 得到當前日期字串 格式(yyyy-MM-dd)
	 */
	public static String getDate() {
		return getDate("yyyy-MM-dd");
	}
	
	/**
	 * 得到當前日期字串 格式(yyyy-MM-dd) pattern可以為:"yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String getDate(String pattern) {
		return DateFormatUtils.format(new Date(), pattern);
	}
	
	/**
	 * 得到日期字串 預設格式(yyyy-MM-dd) pattern可以為:"yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String formatDate(Date date, Object... pattern) {
		String formatDate = null;
		if (pattern != null && pattern.length > 0) {
			formatDate = DateFormatUtils.format(date, pattern[0].toString());
		} else {
			formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
		}
		return formatDate;
	}
	
	/**
	 * 得到日期時間字串,轉換格式(yyyy-MM-dd HH:mm:ss)
	 */
	public static String formatDateTime(Date date) {
		return formatDate(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 得到當前時間字串 格式(HH:mm:ss)
	 */
	public static String getTime() {
		return formatDate(new Date(), "HH:mm:ss");
	}

	/**
	 * 得到當前日期和時間字串 格式(yyyy-MM-dd HH:mm:ss)
	 */
	public static String getDateTime() {
		return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 得到當前年份字串 格式(yyyy)
	 */
	public static String getYear(Date date) {
		if (date == null) {
			return formatDate(new Date(), "yyyy");
		}
		return formatDate(date, "yyyy");
	}

	/**
	 * 得到當前月份字串 格式(MM)
	 */
	public static String getMonth(Date date) {
		if (date == null) {
			return formatDate(new Date(), "MM");
		}
		return formatDate(date, "MM");
	}

	/**
	 * 得到當天字串 格式(dd)
	 */
	public static String getDay(Date date) {
		if (date == null) {
			return formatDate(new Date(), "dd");
		}
		return formatDate(date, "dd");
	}

	/**
	 * 得到當前星期字串 格式(E)星期幾
	 */
	public static String getWeek(Date date) {
		if (date == null) {
			return formatDate(new Date(), "E");
		}
		return formatDate(date, "E");
	}
	
	/**
	 * 日期型字串轉化為日期 格式
	 * { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", 
	 *   "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm",
	 *   "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
	 */
	public static Date parseDate(Object str) {
		if (str == null){
			return null;
		}
		try {
			return parseDate(str.toString(), parsePatterns);
		} catch (ParseException e) {
			return null;
		}
	}
	
	/**
	 * 計算工作時間年
	 */
	public static int pastYears(Date workTime){
		long date = pastDays(workTime);
		int work = (int) (date / 365);
		return work;
	}

	/**
	 * 獲取過去的天數
	 * @param date
	 * @return
	 */
	public static long pastDays(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(24*60*60*1000);
	}

	/**
	 * 獲取過去的小時
	 * @param date
	 * @return
	 */
	public static long pastHour(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(60*60*1000);
	}
	
	/**
	 * 獲取過去的分鐘
	 * @param date
	 * @return
	 */
	public static long pastMinutes(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(60*1000);
	}
	
	/**
	 * 轉換為時間(天,時:分:秒.毫秒)
	 * @param timeMillis
	 * @return
	 */
    public static String formatDateTime(long timeMillis){
		long day = timeMillis/(24*60*60*1000);
		long hour = (timeMillis/(60*60*1000)-day*24);
		long min = ((timeMillis/(60*1000))-day*24*60-hour*60);
		long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60);
		long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000);
		return (day>0?day+",":"")+hour+":"+min+":"+s+"."+sss;
    }
	
	/**
	 * 獲取兩個日期之間的天數
	 * 
	 * @param before
	 * @param after
	 * @return
	 */
	public static double getDistanceOfTwoDate(Date before, Date after) {
		long beforeTime = before.getTime();
		long afterTime = after.getTime();
		return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
	}
	
	/**
	 * String轉date
	 * 
	 * 得到當前日期字串 格式(yyyy-MM-dd) pattern可以為:"yyyy-MM-dd" "HH:mm:ss" "E"
	 *
	 */
	public static Date strToDate(String pattern,String source){
		Date date = new Date();
		try {
			if (pattern == null || "".equals(pattern) ) {
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				date = df.parse(source);
				return date ;
			}
			DateFormat df = new SimpleDateFormat(pattern);
			date = df.parse(source);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * Long轉 date
	 */
	public static Date longToDate(long date){
		Date d = new Date(date);
		return d;
	}
	
	/**
	 * date轉String
	 */
	public static String dateToString(String pattern,Date date){
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}
	
	/**
	 * date 轉long
	 */
	public static long dateToLong(Date date){
		return date.getTime();
	}
	
	/**
	 * 獲取每月的第一天
	 * @param date
	 * @return
	 */
	public static Date getMonthFirstDay(Date date){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar= Calendar.getInstance(); 
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); 
        String first = df.format(calendar.getTime());
        System.out.println("LastMonthDay:"+first);
		return calendar.getTime();	
	}
	
	/**
	 * 獲取每月的最後一天
	 * @param date
	 * @return
	 */
	public static Date getMonthLastDay(Date date){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar= Calendar.getInstance(); 
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));  
        String last = df.format(calendar.getTime());
        System.out.println("LastMonthDay:"+last);
		return calendar.getTime();
	}	
	/**
	 * 判斷是不是週六日
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static boolean isWeek(Date date) throws ParseException {
		boolean flag = false;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || 
        		calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
        	flag = true;
            return flag;
        } 
        return flag;
       
	}
	/**
	 * 包含多少個節假日
	 * @param begin
	 * @param end
	 * @return
	 */
	public static int getHoliday(Date begin,Date end){
		
		return 0;
	}
	
	/**
	 * 倆個日期之間包含多少個週末
	 * @throws ParseException
	 * type true 雙休
	 * false 單休
	 */
	
	public static int getWeekdayCount(Date begin,Date end,boolean type) throws ParseException{
		int count = 0;
		Date flag = begin;
		Calendar calendar = Calendar.getInstance();
		while (flag.compareTo(end) != 1) {
			calendar.setTime(flag);
		    int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;		    //判斷是否為週六日
		    if (type) {
			    if(week == 0 || week == 6){//0為週日,6為週六
			    	calendar.add(Calendar.DAY_OF_MONTH, +1);//跳出迴圈進入下一個日期
			        flag = calendar.getTime();
			        count ++;
			        continue;
			     }
			}else{
		    	if(week == 0){
				  calendar.add(Calendar.DAY_OF_MONTH, +1);
				  flag = calendar.getTime();
				  count ++;
				  continue;
				}
			}
		    calendar.add(Calendar.DAY_OF_MONTH, +1);
		    flag = calendar.getTime();
		}	
		return count;
	}

		
}