package com.chen.util;

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

/**
 * 鏃ユ湡宸ュ叿绫?
 * 
 */
public final class DateUtils {

	private DateUtils() {
	}

	/**
	 * 杞崲鎸囧畾鏍煎紡鐨勫瓧绗︿覆涓烘椂闂?
	 * 
	 * @param str
	 * @param format
	 * @return
	 */
	public static Date formatStr2Date(String str, String pattern) {
		Date d = null;
		if (null != str && !str.trim().equals("")) {
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			try {
				d = sdf.parse(str);
			} catch (ParseException e) {
				throw new RuntimeException(e);
			}
		}
		return d;
	}

	/**
	 * 杞崲鏃堕棿涓烘寚瀹氭牸寮忕殑瀛楃涓?
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDate2Str(Date date, String pattern) {
		if (null == date) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		String s = sdf.format(date);
		return s;
	}

	/**
	 * Calendar杞崲涓篋ate
	 * 
	 * @param calendar
	 * @return
	 */
	public static Date calendar2Date(Calendar calendar) {
		if (null == calendar) {
			return null;
		}
		return calendar.getTime();
	}

	/**
	 * Date杞崲涓篊alendar
	 * 
	 * @param date
	 * @return
	 */
	public static Calendar date2Calendar(Date date) {
		if (null == date) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}

	/**
	 * 璁$畻鎸囧畾鏃ユ湡鏄骞翠腑鐨勭鍑犲懆
	 * 
	 * @param date
	 * @return
	 */
	public static Integer getWeekOfYear(Date date) {
		if (null == date) {
			return 0;
		}
		return date2Calendar(date).get(Calendar.WEEK_OF_YEAR);
	}

	/**
	 * 璁$畻鎸囧畾鏃ユ湡鏄骞翠腑鐨勭鍑犲ぉ
	 * 
	 * @param date
	 * @return
	 */
	public static Integer getDayOfYear(Date date) {
		if (null == date) {
			return 0;
		}
		return date2Calendar(date).get(Calendar.DAY_OF_YEAR);
	}

	/**
	 * 璁$畻鎸囧畾鏃ユ湡鏄鏈堜腑鐨勭鍑犲ぉ
	 * 
	 * @param date
	 * @return
	 */
	public static Integer getDayOfMonth(Date date) {
		if (null == date) {
			return 0;
		}
		return date2Calendar(date).get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 璁$畻鎸囧畾鏃ユ湡鏄湀涓殑绗嚑涓槦鏈?
	 * 
	 * @param date
	 * @return
	 */
	public static Integer getDayOfWeekInMonth(Date date) {
		if (null == date) {
			return 0;
		}
		return date2Calendar(date).get(Calendar.DAY_OF_WEEK_IN_MONTH);
	}

	/**
	 * 璁$畻鎸囧畾鏃ユ湡鏄鍛ㄤ腑鐨勭鍑犲ぉ(鏄熸湡澶?1~~~鏄熸湡鍏?7)
	 * 
	 * @param date
	 * @return
	 */
	public static Integer getDayOfWeek(Date date) {
		if (null == date) {
			return 0;
		}
		return date2Calendar(date).get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 璁$畻鎸囧畾鏃ユ湡鏈堜唤鐨勬渶澶уぉ鏁?
	 * 
	 * @param date
	 * @return
	 */
	public static int getMaxDayInMonth(Date date) {
		if (date == null) {
			return 0;
		}
		return date2Calendar(date).getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 缁欐寚瀹氭棩鏈熸坊鍔犳垨鍑忓幓鎸囧畾骞?
	 * 
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date addYear(Date date, int amount) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, amount);
		return calendar.getTime();
	}

	/**
	 * 鎸囧畾鏃ユ湡鐨勫勾浠?
	 * 
	 * @param date
	 * @param year
	 * @return
	 */
	public static Date setYear(Date date, int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.YEAR, year);
		return calendar.getTime();
	}

	/**
	 * 缁欐寚瀹氭棩鏈熸坊鍔犳垨鍑忓幓鎸囧畾鏈?
	 * 
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date addMonth(Date date, int amount) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, amount);
		return calendar.getTime();
	}

	/**
	 * 鎸囧畾鏃ユ湡鏈堜唤
	 * 
	 * @param date
	 * @param month
	 * @return
	 */
	public static Date setMonth(Date date, int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MONTH, month);
		return calendar.getTime();
	}

	/**
	 * 缁欐寚瀹氭棩鏈熸坊鍔犳垨鍑忓幓鎸囧畾鍛?
	 * 
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date addWeek(Date date, int amount) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.WEEK_OF_MONTH, amount);
		return calendar.getTime();
	}

	/**
	 * 缁欐寚瀹氭棩鏈熸坊鍔犳垨鍑忓幓鎸囧畾澶?
	 * 
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date addDay(Date date, int amount) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, amount);
		return calendar.getTime();
	}

	/**
	 * 鎸囧畾鏃ユ湡鐨勫ぉ鏁?
	 * 
	 * @param date
	 * @param day
	 * @return
	 */
	public static Date setDay(Date date, int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, day);
		return calendar.getTime();
	}

	/**
	 * 缁欐寚瀹氭棩鏈熸坊鍔犳垨鍑忓幓鎸囧畾灏忔椂
	 * 
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date addHour(Date date, int amount) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR_OF_DAY, amount);
		return calendar.getTime();
	}

	/**
	 * 鎸囧畾鏃ユ湡鐨勫皬鏃?
	 * 
	 * @param date
	 * @param hour
	 * @return
	 */
	public static Date setHour(Date date, int hour) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		return calendar.getTime();
	}

	/**
	 * 缁欐寚瀹氭棩鏈熸坊鍔犳垨鍑忓幓鎸囧畾鍒嗛挓
	 * 
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date addMinute(Date date, int amount) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, amount);
		return calendar.getTime();
	}

	/**
	 * 鎸囧畾鏃ユ湡鐨勫垎閽?
	 * 
	 * @param date
	 * @param minute
	 * @return
	 */
	public static Date setMinute(Date date, int minute) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MINUTE, minute);
		return calendar.getTime();
	}

	/**
	 * 缁欐寚瀹氭棩鏈熸坊鍔犳垨鍑忓幓鎸囧畾绉?
	 * 
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date addSecond(Date date, int amount) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.SECOND, amount);
		return calendar.getTime();
	}

	/**
	 * 鎸囧畾鏃ユ湡鐨勭
	 * 
	 * @param date
	 * @param second
	 * @return
	 */
	public static Date setSecond(Date date, int second) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.SECOND, second);
		return calendar.getTime();
	}

	/**
	 * 缁欐寚瀹氭棩鏈熸坊鍔犳垨鍑忓幓鎸囧畾姣
	 * 
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date addMillisecond(Date date, int amount) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MILLISECOND, amount);
		return calendar.getTime();
	}

	/**
	 * 鎸囧畾鏃ユ湡鐨勬绉?
	 * 
	 * @param date
	 * @param millisecond
	 * @return
	 */
	public static Date setMillisecond(Date date, int millisecond) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MILLISECOND, millisecond);
		return calendar.getTime();
	}

	/**
	 * 璁$畻涓や釜鏃ユ湡涔嬮棿宸殑澶氬皯姣锛屽鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫悗闈㈠垯杩斿洖涓?涓鏁般?佸鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫墠闈㈠垯杩斿洖涓?涓礋鏁般?佷袱涓棩鏈熺浉鍚岃繑鍥?0
	 * 
	 * @param firstDate
	 * @param secondDate
	 * @return
	 */
	public static long compareMillisecond(Date firstDate, Date secondDate) {
		long time1 = firstDate.getTime();
		long time2 = secondDate.getTime();
		return (time1 - time2);
	}

	/**
	 * 璁$畻涓や釜鏃ユ湡涔嬮棿宸殑澶氬皯绉掞紝濡傛灉鏃ユ湡firstDate鍦ㄦ棩鏈焥econdDate鐨勫悗闈㈠垯杩斿洖涓?涓鏁般?佸鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫墠闈㈠垯杩斿洖涓?涓礋鏁般?佷袱涓棩鏈熺浉鍚岃繑鍥?0
	 * 
	 * @param firstDate
	 * @param secondDate
	 * @return
	 */
	public static long compareSecond(Date firstDate, Date secondDate) {
		long time1 = firstDate.getTime();
		long time2 = secondDate.getTime();
		return ((time1 - time2) / (1000));
	}

	/**
	 * 璁$畻涓や釜鏃ユ湡涔嬮棿宸殑澶氬皯鍒嗛挓锛屽鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫悗闈㈠垯杩斿洖涓?涓鏁般?佸鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫墠闈㈠垯杩斿洖涓?涓礋鏁般?佷袱涓棩鏈熺浉鍚岃繑鍥?0
	 * 
	 * @param firstDate
	 * @param secondDate
	 * @return
	 */
	public static long compareMinute(Date firstDate, Date secondDate) {
		long time1 = firstDate.getTime();
		long time2 = secondDate.getTime();
		return ((time1 - time2) / (1000 * 60));
	}

	/**
	 * 璁$畻涓や釜鏃ユ湡涔嬮棿宸殑澶氬皯灏忔椂锛屽鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫悗闈㈠垯杩斿洖涓?涓鏁般?佸鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫墠闈㈠垯杩斿洖涓?涓礋鏁般?佷袱涓棩鏈熺浉鍚岃繑鍥?0
	 * 
	 * @param firstDate
	 * @param secondDate
	 * @return
	 */
	public static long compareHour(Date firstDate, Date secondDate) {
		long time1 = firstDate.getTime();
		long time2 = secondDate.getTime();
		return ((time1 - time2) / (1000 * 60 * 60));
	}

	/**
	 * 璁$畻涓や釜鏃ユ湡涔嬮棿宸殑澶氬皯澶╋紝濡傛灉鏃ユ湡firstDate鍦ㄦ棩鏈焥econdDate鐨勫悗闈㈠垯杩斿洖涓?涓鏁般?佸鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫墠闈㈠垯杩斿洖涓?涓礋鏁般?佷袱涓棩鏈熺浉鍚岃繑鍥?0
	 * 
	 * @param firstDate
	 * @param secondDate
	 * @return
	 */
	public static long compareDay(Date firstDate, Date secondDate) {
		long time1 = firstDate.getTime();
		long time2 = secondDate.getTime();
		return ((time1 - time2) / (1000 * 60 * 60 * 24));
	}

	// /**
	// *
	// 璁$畻涓や釜鏃ユ湡涔嬮棿宸殑澶氬皯鏈堬紝濡傛灉鏃ユ湡firstDate鍦ㄦ棩鏈焥econdDate鐨勫悗闈㈠垯杩斿洖涓?涓鏁般?佸鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫墠闈㈠垯杩斿洖涓?涓礋鏁般?佷袱涓棩鏈熺浉鍚岃繑鍥?0
	// *
	// * @param firstDate
	// * @param secondDate
	// * @return
	// */
	// public static long compareMonth(Date start, Date end) {
	// // TODO
	// if (start.after(end)) {
	// Date t = start;
	// start = end;
	// end = t;
	// }
	// Calendar startCalendar = Calendar.getInstance();
	// startCalendar.setTime(start);
	// Calendar endCalendar = Calendar.getInstance();
	// endCalendar.setTime(end);
	// Calendar temp = Calendar.getInstance();
	// temp.setTime(end);
	// temp.add(Calendar.DATE, 1);
	//
	// int year = endCalendar.get(Calendar.YEAR)
	// - startCalendar.get(Calendar.YEAR);
	// int month = endCalendar.get(Calendar.MONTH)
	// - startCalendar.get(Calendar.MONTH);
	//
	// if ((startCalendar.get(Calendar.DATE) == 1)
	// && (temp.get(Calendar.DATE) == 1)) {
	// return year * 12 + month + 1;
	// } else if ((startCalendar.get(Calendar.DATE) != 1)
	// && (temp.get(Calendar.DATE) == 1)) {
	// return year * 12 + month;
	// } else if ((startCalendar.get(Calendar.DATE) == 1)
	// && (temp.get(Calendar.DATE) != 1)) {
	// return year * 12 + month;
	// } else {
	// return (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month);
	// }
	// }

	// /**
	// *
	// 璁$畻涓や釜鏃ユ湡涔嬮棿宸殑澶氬皯骞达紝濡傛灉鏃ユ湡firstDate鍦ㄦ棩鏈焥econdDate鐨勫悗闈㈠垯杩斿洖涓?涓鏁般?佸鏋滄棩鏈焒irstDate鍦ㄦ棩鏈焥econdDate鐨勫墠闈㈠垯杩斿洖涓?涓礋鏁般?佷袱涓棩鏈熺浉鍚岃繑鍥?0
	// *
	// * @param firstDate
	// * @param secondDate
	// * @return
	// */
	// public static long compareYear(Date start, Date end) {
	// // TODO
	// return 0;
	// }

	/**
	 * 鑾峰彇鎸囧畾鏃ユ湡瀵瑰簲鐨勫勾浠界殑绗竴澶╃殑鏃ユ湡
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_YEAR, 1);// 璁句负鏃ユ湡涓轰粖骞寸殑绗?1澶?
		return calendar.getTime();
	}

	/**
	 * 鑾峰彇鎸囧畾鏃ユ湡瀵瑰簲鐨勫勾浠界殑鏈?鍚庝竴澶╃殑鏃ユ湡
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_YEAR, 1);// 璁句负鏃ユ湡涓轰粖骞寸殑绗?1澶?
		calendar.add(Calendar.YEAR, 1);// 鍔犱笂涓?骞达紝鍒扮浜屽勾鐨勭涓?澶?
		calendar.add(Calendar.DATE, -1);// 鍑忎竴澶╋紝浠婂勾鐨勬渶鍚庝竴澶?
		return calendar.getTime();
	}

	/**
	 * 鑾峰彇鎸囧畾鏃ユ湡瀵瑰簲鐨勫搴︾殑绗竴澶╃殑鏃ユ湡
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfQuarter(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int month = calendar.get(Calendar.MONTH);
		if (0 == month || 1 == month || 2 == month) {
			calendar.set(Calendar.MONDAY, 0);
		} else if (3 == month || 4 == month || 5 == month) {
			calendar.set(Calendar.MONDAY, 3);
		} else if (6 == month || 7 == month || 8 == month) {
			calendar.set(Calendar.MONDAY, 6);
		} else {
			calendar.set(Calendar.MONDAY, 9);
		}
		return getFirstDayOfMonth(calendar.getTime());
	}

	/**
	 * 鑾峰彇鎸囧畾鏃ユ湡瀵瑰簲鐨勫搴︾殑鏈?鍚庝竴澶╃殑鏃ユ湡
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfQuarter(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int month = calendar.get(Calendar.MONTH);
		if (0 == month || 1 == month || 2 == month) {
			calendar.set(Calendar.MONDAY, 2);
		} else if (3 == month || 4 == month || 5 == month) {
			calendar.set(Calendar.MONDAY, 5);
		} else if (6 == month || 7 == month || 8 == month) {
			calendar.set(Calendar.MONDAY, 8);
		} else {
			calendar.set(Calendar.MONDAY, 11);
		}
		return getLastDayOfMonth(calendar.getTime());
	}

	/**
	 * 鑾峰彇鎸囧畾鏃ユ湡瀵瑰簲鐨勬湀浠界殑绗竴澶╃殑鏃ユ湡
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, 1);// 璁句负褰撳墠鏈堢殑1鍙?
		return calendar.getTime();
	}

	/**
	 * 鑾峰彇鎸囧畾鏃ユ湡瀵瑰簲鐨勬湀浠界殑鏈?鍚庝竴澶╃殑鏃ユ湡
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, 1);// 璁句负褰撳墠鏈堢殑1鍙?
		calendar.add(Calendar.MONTH, 1);// 鍔犱竴涓湀锛屽彉涓轰笅鏈堢殑1鍙?
		calendar.add(Calendar.DATE, -1);// 鍑忓幓涓?澶╋紝鍙樹负褰撴湀鏈?鍚庝竴澶?
		return calendar.getTime();
	}

	/**
	 * 鑾峰彇鎸囧畾鏃ユ湡瀵瑰簲鐨勫懆鐨勭涓?澶╃殑鏃ユ湡(鎸変腑鍥戒範鎯槦鏈熶竴浣滀负涓?鍛ㄧ殑绗竴澶?)
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfWeek(Date date) {
		int dayOfWeek = getDayOfWeek(date);
		if (2 == dayOfWeek) {
			return date;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		while (true) {
			calendar.add(Calendar.DATE, -1);
			if (2 == getDayOfWeek(calendar.getTime())) {
				break;
			}
		}
		return calendar.getTime();
	}

	/**
	 * 鑾峰彇鎸囧畾鏃ユ湡瀵瑰簲鐨勫懆鐨勬渶鍚庝竴澶╃殑鏃ユ湡(鎸変腑鍥戒範鎯槦鏈熷ぉ浣滀负涓?鍛ㄧ殑鏈?鍚庝竴澶?)
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfWeek(Date date) {
		int dayOfWeek = getDayOfWeek(date);
		if (1 == dayOfWeek) {
			return date;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		while (true) {
			calendar.add(Calendar.DATE, 1);
			if (1 == getDayOfWeek(calendar.getTime())) {
				break;
			}
		}
		return calendar.getTime();
	}
}
最近下载更多
twingo0204  LV24 2016年1月6日
levitation  LV7 2012年8月23日
最近浏览更多
13063925092  LV1 2020年5月15日
能不能不存在  LV13 2020年4月21日
fpxrng  LV9 2019年12月12日
hzhsh0223  LV18 2019年4月13日
fly666  LV11 2018年9月27日
lw19900921  LV25 2018年9月20日
李杰强  LV18 2018年6月24日
jd_lzh  LV1 2018年5月31日
eeeerer  LV9 2018年4月25日
deng214  LV9 2018年2月2日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友