package com.tzld.piaoquan.growth.common.utils;

import com.stuuudy.commons.util.date.CalendarUtil;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 日期工具类
 * 
 * @author AKuo
 * @since 1.0
 * @date 2014-4-29 下午3:28:04
 */
public abstract class DateUtils {

	private static final long ONE_MINUTE = 60;
	private static final long ONE_HOUR = 3600;
	private static final long ONE_DAY = 86400;
	private static final long ONE_MONTH = 2592000;
	private static final long ONE_YEAR = 31104000;
	public static final String PATTERN_ONLY_DATE = "yyyy-MM-dd";
	private static final String PATTERN_ONLY_TIME = "HH:mm:ss";
	private static final String PATTERN_DATETIME = "yyyy-MM-dd HH:mm:ss";
	public static final String yyyyMMdd_HHmmss= "yyyy-MM-dd HH:mm:ss";

	public static final String yyyyMMdd = "yyyyMMdd";

	/**
	 * 日期对象转字符串，用户指定格式化对象
	 * 
	 * @param date
	 *            日期对象
	 * @param dateFormat
	 *            格式化对象
	 * @return
	 */
	public static String dateToString(Date date, SimpleDateFormat dateFormat) {
		if (date == null) {
			throw new IllegalArgumentException("date can't be null");
		}
		return dateFormat.format(date);
	}

	/**
	 * 日期对象转字符串，用户指定格式
	 * 
	 * @param date
	 *            日期对象
	 * @param pattern
	 *            格式化
	 * @return
	 */
	public static String dateToString(Date date, String pattern) {
		return dateToString(date, getSimpleDateFormat(pattern));
	}

	/**
	 * 字符串转日期对象，用户指定格式化对象
	 * 
	 * @param datestr
	 *            日期字符串
	 * @param dateFormat
	 *            格式化对象
	 * @return Date
	 * @throws ParseException
	 *             解析异常
	 */
	public static Date parse(String datestr, SimpleDateFormat dateFormat) throws ParseException {
		if (datestr == null) {
			throw new IllegalArgumentException("datestr can't be null");
		}
		return dateFormat.parse(datestr);
	}

	/**
	 * 字符串转日期对象，用户指定格式
	 * 
	 * @param datestr
	 *            日期字符串
	 * @param pattern
	 *            格式化
	 * @return Date
	 * @throws ParseException
	 *             解析异常
	 */
	public static Date parse(String datestr, String pattern) throws ParseException {
		return parse(datestr, getSimpleDateFormat(pattern));
	}

	/**
	 * 根据格式获取格式化对象
	 * 
	 * @param pattern
	 * @return
	 */
	private static SimpleDateFormat getSimpleDateFormat(String pattern) {
		if (pattern == null) {
			throw new IllegalArgumentException("pattern can't be null");
		}
		return new SimpleDateFormat(pattern);
	}

	/**
	 * 日期转字符串，格式：yyyy-MM-dd
	 * 
	 * @param date
	 *            时间值
	 * @return
	 */
	public static String dateToString_only_date(Date date) {
		return dateToString(date, PATTERN_ONLY_DATE);
	}

	/**
	 * 当前日期转字符串，格式：yyyy-MM-dd
	 * 
	 * @return
	 */
	public static String nowToString_only_date() {
		return dateToString_only_date(new Date());
	}

	/**
	 * 字符串转日期，格式：yyyy-MM-dd
	 * 
	 * @param strDate
	 *            日期字符串
	 * @return Date
	 * @throws ParseException
	 *             解析异常
	 */
	public static Date parse_only_date(String strDate) throws ParseException {
		return parse(strDate, PATTERN_ONLY_DATE);
	}

	/**
	 * 日期转字符串，格式：HH:mm:ss
	 * 
	 * @param date
	 *            时间值
	 * @return
	 */
	public static String dateToString_only_time(Date date) {
		return dateToString(date, PATTERN_ONLY_TIME);
	}

	/**
	 * 当前日期转字符串，格式：HH:mm:ss
	 * 
	 * @return
	 */
	public static String nowToString_only_time() {
		return dateToString_only_time(new Date());
	}

	/**
	 * 字符串转日期，格式：HH:mm:ss
	 * 
	 * @param strDate
	 *            日期字符串
	 * @return Date
	 * @throws ParseException
	 *             解析异常
	 */
	public static Date parse_only_time(String strDate) throws ParseException {
		return parse(strDate, PATTERN_ONLY_TIME);
	}

	/**
	 * 日期转字符串，格式：yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 *            时间值
	 * @return
	 */
	public static String dateToString(Date date) {
		return dateToString(date, PATTERN_DATETIME);
	}

	/**
	 * 当前日期转字符串，格式：yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	public static String nowToString() {
		return dateToString(new Date());
	}

	/**
	 * 字符串转日期，格式：yyyy-MM-dd HH:mm:ss
	 * 
	 * @param strDate
	 *            日期字符串
	 * @return Date
	 * @throws ParseException
	 *             解析异常
	 */
	public static Date parse(String strDate) throws ParseException {
		return parse(strDate, PATTERN_DATETIME);
	}

	/**
	 * 多少年以前
	 * 
	 * @param manyYears
	 *            多少年,传正数
	 * @return
	 */
	public static Date beforeManyYears(Integer manyYears) {
		return beforeManyYears(null, manyYears);
	}

	/**
	 * 多少天以前
	 * 
	 * @param manyDays
	 *            多少天,传正数
	 * @return
	 */
	public static Date beforeManyDays(Integer manyDays) {
		return beforeManyDays(null, manyDays);
	}

	/**
	 * 多少年以前
	 * 
	 * @param date
	 *            日期对象，不传默认是当前
	 * @param manyYears
	 *            多少年,传正数
	 * @return
	 */
	public static Date beforeManyYears(Date date, Integer manyYears) {
		if (manyYears > 0) {
			manyYears = 0 - manyYears;
		}
		Calendar calendar = Calendar.getInstance();
		if (date != null) {
			calendar.setTime(date);
		}
		calendar.add(Calendar.YEAR, manyYears);
		return calendar.getTime();
	}

	/**
	 * 多少天以前
	 * 
	 * @param date
	 *            日期对象，不传默认是当前
	 * @param manyDays
	 *            多少天,传正数
	 * @return
	 */
	public static Date beforeManyDays(Date date, Integer manyDays) {
		if (manyDays > 0) {
			manyDays = 0 - manyDays;
		}
		Calendar calendar = Calendar.getInstance();
		if (date != null) {
			calendar.setTime(date);
		}
		calendar.add(Calendar.DAY_OF_YEAR, manyDays);
		return calendar.getTime();
	}

	/**
	 * 多少年以后
	 * 
	 * @param manyYears
	 *            多少年,传正数
	 * @return
	 */
	public static Date afterManyYears(Integer manyYears) {
		return afterManyYears(null, manyYears);
	}

	/**
	 * 多少天以后
	 * 
	 * @param manyDays
	 *            多少天,传正数
	 * @return
	 */
	public static Date afterManyDays(Integer manyDays) {
		return afterManyDays(null, manyDays);
	}

	/**
	 * 多少年以后
	 * 
	 * @param date
	 *            日期对象，不传默认是当前
	 * @param manyYears
	 *            多少年,传正数
	 * @return
	 */
	public static Date afterManyYears(Date date, Integer manyYears) {
		if (manyYears < 0) {
			manyYears = 0 - manyYears;
		}
		Calendar calendar = Calendar.getInstance();
		if (date != null) {
			calendar.setTime(date);
		}
		calendar.add(Calendar.YEAR, manyYears);
		return calendar.getTime();
	}

	/**
	 * 多少天以后
	 * 
	 * @param date
	 *            日期对象，不传默认是当前
	 * @param manyDays
	 *            多少天,传正数
	 * @return
	 */
	public static Date afterManyDays(Date date, Integer manyDays) {
		if (manyDays < 0) {
			manyDays = 0 - manyDays;
		}
		Calendar calendar = Calendar.getInstance();
		if (date != null) {
			calendar.setTime(date);
		}
		calendar.add(Calendar.DAY_OF_YEAR, manyDays);
		return calendar.getTime();
	}

	/**
	 * 当天开始的时间
	 * 
	 * @return
	 */
	public static Date todayBegin() {
		return dayBegin(new Date());
	}

	/**
	 * 获得某个日期对象的开始时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date dayBegin(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 当天结束的时间
	 * 
	 * @return
	 */
	public static Date todayEnd() {
		return dayEnd(new Date());
	}

	/**
	 * 获得某个日期对象的结束时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date dayEnd(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar.getTime();
	}

	/**
	 * 日期偏移，返回格式:yyyyMMdd
	 * 
	 * @param date
	 * @param offsetday
	 * @return
	 */
	public static Date getDateByDayOffset(Date date, int offset) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_YEAR, offset);
		return calendar.getTime();
	}

	/**
	 * 日期偏移
	 * 
	 * @param date
	 *            要偏移的日期
	 * @param type
	 *            偏移的位置
	 * @param offset
	 *            偏移量
	 * @return
	 */
	public static Date getDateByDayOffset(Date date, int type, int offset) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(type, offset);
		return calendar.getTime();
	}

	private static final long base = 1388505600000l;// 1970-2014年的毫秒数，做为基数被减掉，以减少长度

	/**
	 * 获取2014年1月1日到现在的秒数
	 * 
	 * @return String
	 */
	public static String getMyUnixSecStr() {
		return getMyUnixSec().toString();
	}

	/**
	 * 获取unix时间戳
	 * 
	 * @return
	 */
	public static long getUnixSec() {
		return System.currentTimeMillis() / 1000;
	}

	/**
	 * 获取2014年1月1日到现在的秒数
	 * 
	 * @return Long
	 */
	public static Long getMyUnixSec() {
		return System.currentTimeMillis() - base;
	}

	/**
	 * 获取2014年1月1日到现在的秒数
	 * 
	 * @return Long
	 */
	public static Long getMyUnixSec(Date date) {
		return date.getTime() - base;
	}

	/**
	 * 获取当前时间前几天的时间
	 * 
	 * @param now
	 * @param timing
	 * @return
	 */
	public static Date getBeforeTimeOfDay(Date now, int timing) {
		Calendar c = Calendar.getInstance();
		c.setTime(now);
		c.add(Calendar.DAY_OF_YEAR, 0 - timing);
		return c.getTime();
	}

	/**
	 * 获取当前时间前几天的时间-具体到秒
	 * 
	 * @param now
	 * @param timing
	 * @return
	 */
	public static Date getBeforeDateOfTime(Date now, int timing) {
		Calendar c = Calendar.getInstance();
		c.setTime(now);
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE) - timing, c.get(Calendar.HOUR_OF_DAY),
				c.get(Calendar.MINUTE), c.get(Calendar.SECOND));
		return c.getTime();
	}

	private static long ns = 1000; // 一秒钟的毫秒数

	/**
	 * 毫秒转具体秒数
	 * 
	 * @param time
	 * @return
	 */
	public static long getdiffTimeSec(long time) {
		return time / ns;
	}

	/**
	 * 获取日期目录形式的字符串
	 * 
	 * @return String yyyy-MM-dd
	 */
	public static Date nowDate() {
		Calendar cal_now = Calendar.getInstance();
		return cal_now.getTime();
	}

	private static final SimpleDateFormat format_date_Hour = new SimpleDateFormat("HH");

	public static int getDateHour() throws Exception {
		String hour = format_date_Hour.format(new Date());
		return Integer.parseInt(hour);
	}

	private static final SimpleDateFormat format_date_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 某日期的开始
	 * 
	 * @param date
	 * @return
	 */
	public static Date dayTimeBegin(String date) {
		Date r = null;
		try {
			r = format_date_time.parse(date + " 00:00:00");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return r;
	}

	/**
	 * 某日期的结束
	 * 
	 * @param date
	 * @return
	 */
	public static Date dayTimeEnd(String date) {
		Date r = null;
		try {
			r = format_date_time.parse(date + " 23:59:59");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return r;
	}

	/**
	 * 获取当前时间后几小时的时间
	 * 
	 * @param now
	 * @param timing
	 * @return
	 */
	public static Date getAfterTimeOfHours(Date now, int timing) {
		Calendar c = Calendar.getInstance();
		c.setTime(now);
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), c.get(Calendar.HOUR_OF_DAY) + timing,
				c.get(Calendar.MINUTE), c.get(Calendar.SECOND));
		return c.getTime();
	}

	/**
	 * 获取日期目录形式的字符串
	 * 
	 * @return Date yyyy-MM-dd HH:mm:ss->yyyy-MM-dd
	 */
	public static Date dateToDay(Date date) {
		try {
			String sdate = dateString(date);
			return formatDateDay(sdate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static final SimpleDateFormat format_date = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * 获取日期目录形式的字符串
	 * 
	 * @param date
	 * @return String yyyy-MM-dd
	 */
	public static String dateString(Date date) {
		return format_date.format(date);
	}

	/**
	 * 将字符串转换成标准时间格式
	 * 
	 * @param date
	 *            yyyy-MM-dd
	 * @return
	 */
	public static Date formatDateDay(String date) {
		Date new_date = null;
		try {
			new_date = format_date.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new_date;
	}

	public static Date combinationDateTime(Date d, Date t) {
		String date = dateString(d);
		String time = dateToStrTime(t);
		return formatMinutes(date + " " + time);
	}

	private static final SimpleDateFormat format_time = new SimpleDateFormat("HH:mm:ss");

	/**
	 * 获取日期目录形式的字符串
	 * 
	 * @param date
	 * @return String HH:mm:ss
	 */
	public static String dateToStrTime(Date time) {
		return format_time.format(time);
	}

	private static final SimpleDateFormat format_date_minutes = new SimpleDateFormat("yyyy-MM-dd HH:mm");

	/**
	 * 将字符串转换成标准时间格式
	 * 
	 * @param date
	 *            yyyy-MM-dd HH:mm
	 * @return
	 */
	public static Date formatMinutes(String date) {
		Date new_date = null;
		try {
			new_date = format_date_minutes.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new_date;
	}

	/**
	 * 计算某个日期到当前日期之间相差的天数
	 * 
	 * @param smdate
	 *            较小的时间
	 * @param bdate
	 *            较大的时间
	 * @return List<Date> 相差天数
	 */
	public static List<Date> daysBetweenNowDate(Date smdate, Date bdate) {
		if (bdate == null || smdate == null || bdate.getTime() < smdate.getTime()) {
			throw new IllegalArgumentException();
		}
		List<Date> dates = new ArrayList<Date>();
		Calendar cal_sm = Calendar.getInstance();
		cal_sm.setTime(smdate);
		Calendar cal_big = Calendar.getInstance();
		cal_big.setTime(bdate);
		Calendar cal_now = Calendar.getInstance();
		while (dateCompare(cal_now, cal_sm)) {
			dates.add(cal_sm.getTime());
			cal_sm.add(Calendar.DAY_OF_YEAR, +1);
			if (dateCompare(cal_sm, cal_big)) {
				break;
			}
		}
		return dates;
	}

	/**
	 * 把日期集合转换成字符串集合
	 * 
	 * @param List
	 *            <Date>
	 * @return List<String> yyyy-MM-dd
	 */
	public static List<String> dateStringList(List<Date> datelist) {
		List<String> list = new ArrayList<String>();
		for (Date date : datelist) {
			list.add(format_date.format(date));
		}
		return list;
	}

	private static final SimpleDateFormat format_month = new SimpleDateFormat("yyyy-MM");

	/**
	 * 将字符串转换成标准时间格式
	 * 
	 * @param date
	 *            yyyy-MM
	 * @return
	 */
	public static Date formatDateMonth(String date) {
		Date new_date = null;
		try {
			new_date = format_month.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new_date;
	}

	/**
	 * 返回指定日期的月的第一天
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static Date getFirstDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 返回指定日期的月的最后一天
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static Date getLastDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
		calendar.roll(Calendar.DATE, -1);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}

	/**
	 * 将当前时间转换成标准时间格式
	 * 
	 * @param date
	 *            yyyy-MM-dd
	 * @return
	 */
	public static Date dateToDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获取当前时间后几分钟的时间
	 * 
	 * @param now
	 * @param timing
	 * @return
	 */
	public static Date getAfterTimeOfMinutes(Date now, int timing) {
		Calendar c = Calendar.getInstance();
		c.setTime(now);
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), c.get(Calendar.HOUR_OF_DAY),
				c.get(Calendar.MINUTE) + timing, 0);
		return c.getTime();
	}

	/**
	 * 将当前时间转换成标准时间格式
	 * 
	 * @param date
	 *            yyyy-MM-dd HH:mm
	 * @return
	 */
	public static Date dateToDateMinutes(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获取当前时间前几小时的时间
	 * 
	 * @param now
	 * @param timing
	 * @return
	 */
	public static Date getBeforeTimeOfHours(Date now, int timing) {
		Calendar c = Calendar.getInstance();
		c.setTime(now);
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), c.get(Calendar.HOUR_OF_DAY) - timing,
				c.get(Calendar.MINUTE), 0);
		return c.getTime();
	}

	/**
	 * 获取当前时间前几天的时间-具体到分钟
	 * 
	 * @param now
	 * @param timing
	 * @return
	 */
	public static Date getBeforeTimeOfDate(Date now, int timing) {
		Calendar c = Calendar.getInstance();
		c.setTime(now);
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE) - timing, c.get(Calendar.HOUR_OF_DAY),
				c.get(Calendar.MINUTE), 0);
		return c.getTime();
	}

	/**
	 * 上一小时开始的时间
	 * 
	 * @return
	 */
	public static Date preHourBegin() {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), c.get(Calendar.HOUR_OF_DAY) - 1, 0, 0);
		return c.getTime();
	}

	/**
	 * 上一小时结束的时间
	 * 
	 * @return
	 */
	public static Date preHourEnd() {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), c.get(Calendar.HOUR_OF_DAY) - 1, 59,
				59);
		return c.getTime();
	}

	/**
	 * 上一小时开始的时间
	 *
	 * @return
	 */
	public static Date preHourBegin(Integer hour) {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), c.get(Calendar.HOUR_OF_DAY) - hour, 0, 0);
		return c.getTime();
	}

	/**
	 * 上一小时结束的时间
	 *
	 * @return
	 */
	public static Date preHourEnd(Integer hour) {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), c.get(Calendar.HOUR_OF_DAY) -hour, 59,
				59);
		return c.getTime();
	}


	/**
	 * 上一天开始的时间
	 * 
	 * @return
	 */
	public static Date preDayBegin() {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE) - 1, 0, 0, 0);
		return c.getTime();
	}

	/**
	 * 上一天结束的时间
	 * 
	 * @return
	 */
	public static Date preDayEnd() {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE) - 1, 23, 59, 59);
		return c.getTime();
	}

	public static Date getMonday() {
		Calendar calendar = Calendar.getInstance();
		while (calendar.get(Calendar.DAY_OF_WEEK) != 2) {
			calendar.add(Calendar.DAY_OF_YEAR, -1);
		}
		return calendar.getTime();
	}

	private static final SimpleDateFormat format_only_date = new SimpleDateFormat("yyyyMMdd");

	/**
	 * 日期偏移，返回格式:yyyyMMdd
	 * 
	 * @param date
	 * @param offsetday
	 * @return
	 */
	public static String dayOffset(Date date, int offset) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_YEAR, offset);
		return format_only_date.format(calendar.getTime());
	}

	/**
	 * 根据用户生日计算年龄
	 */
	public static int getAgeByBirthday(Date birthday) {
		Calendar cal = Calendar.getInstance();
		if (cal.before(birthday)) {
			throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
		}
		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH) + 1;
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
		cal.setTime(birthday);
		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH) + 1;
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
		int age = yearNow - yearBirth;
		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				// monthNow==monthBirth
				if (dayOfMonthNow < dayOfMonthBirth) {
					age--;
				}
			} else {
				// monthNow>monthBirth
				age--;
			}
		}
		return age;
	}

	/**
	 * 计算两个日期之间相差的天数
	 * 
	 * @param smdate
	 *            较小的时间
	 * @param bdate
	 *            较大的时间
	 * @return List<Date> 相差天数
	 */
	public static List<Date> daysBetween(Date smdate, Date bdate) {
		List<Date> dates = new ArrayList<Date>();
		if (bdate == null || smdate == null || bdate.getTime() < smdate.getTime()) {
			return dates;
		}
		Calendar cal_sm = Calendar.getInstance();
		cal_sm.setTime(smdate);
		Calendar cal_big = Calendar.getInstance();
		cal_big.setTime(bdate);
		while (dateCompare(cal_big, cal_sm)) {
			dates.add(cal_sm.getTime());
			cal_sm.add(Calendar.DAY_OF_YEAR, +1);
		}
		return dates;
	}

	/**
	 * 对比两个日历中的日期哪个大，不对比时间
	 * 
	 * @param c1
	 *            日历1
	 * @param c2
	 *            日历2
	 * @return 返回c1是否比c2大
	 */
	private static boolean dateCompare(Calendar c1, Calendar c2) {
		return c1.get(Calendar.YEAR) > c2.get(Calendar.YEAR) || (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)
				&& c1.get(Calendar.DAY_OF_YEAR) > c2.get(Calendar.DAY_OF_YEAR));
	}

	public static String formatIso8601Date(Date date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
		df.setTimeZone(new SimpleTimeZone(8, "GMT"));
		return df.format(date);
	}

	/**
	 * 日期型转化成字符串类型
	 * 
	 * @param format
	 * @param date
	 * @return
	 */
	public static String dateToStringyyyyMMdd(Date date) {
		return new SimpleDateFormat(yyyyMMdd).format(date);
	}
	
	public static Date hourPlus(Date date,int hours ) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR, hours);
		return calendar.getTime();
	}

	public static Date dayPlus(Date date, int days) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, days);
		return calendar.getTime();
	}

	public static Date minutePlus(Date date, int minutes) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, minutes);
		return calendar.getTime();
	}

	public static Date secondPlus(Date date, int seconds) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.SECOND, seconds);
		return calendar.getTime();
	}
	
	public static String formatDateByPattern(String format, Date date) {
		SimpleDateFormat simple = new SimpleDateFormat(format);
		return simple.format(date);
	}
	public static void main(String[] args) throws  Exception{
		System.out.println(preHourBegin(14));
		System.out.println(preHourEnd(14));
		Date gmtModified = new Date();
		System.out.println(DateUtils.dateToString(gmtModified ,"MM月dd日HH:mm"));

		System.out.println(dateToString_only_date(dayPlus(new Date(),7)));

	}

	public static boolean isSameDay(Date gmtCreate, Date currentDatetime) {

		return org.apache.commons.lang.time.DateUtils.isSameDay(gmtCreate, currentDatetime);
	}



	/**
	 * 距离现在多久
	 *
	 * @param date
	 * @return
	 *
	 */
	public static String fromTodayDescr(Date date) {
		Calendar calendar = Calendar.getInstance();
		int nowYear = calendar.get(Calendar.YEAR);
		int nowDate = calendar.get(Calendar.DATE);
		calendar.setTime(date);
		int timeYear = calendar.get(Calendar.YEAR);
		int timeDate = calendar.get(Calendar.DATE);
		long time = date.getTime() / 1000;
		long now = new Date().getTime() / 1000;
		long ago = now - time;
		if (ago < 0) {
			ago = 1;
		}
		if (ago <= ONE_MINUTE)
			return "1分钟内";
		else if (ago < 3600) {
			return ago / ONE_MINUTE + "分钟前";
		} else if (ago < ONE_DAY) {
			if (nowDate == timeDate) {
				return ago / ONE_HOUR + "小时前";
			}
		}
		if (ago < ONE_DAY * 2) {
			Date beginToday = CalendarUtil.getBeginTimeOfAddDays(new Date(), 0);
			Date beginYesterday = CalendarUtil.getBeginTimeOfAddDays(new Date(), -1);
			if (date.after(beginYesterday) && date.before(beginToday)) {
				return "昨天 " + formatDateByPattern("HH:mm", date);
			}
		}
		if (nowYear == timeYear) {
			return formatDateByPattern("MM-dd HH:mm", date);
		} else {
			return formatDateByPattern("yyyy-MM-dd HH:mm", date);
		}
	}

	public static Date longToDate(String millis) {
		if (StringUtils.isBlank(millis)) {
			return new Date();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(Long.parseLong(millis));
		Date date = cal.getTime();
		return date;
	}
	public static String longToDate(Long second,String patten) {
		if(second==null){
			return "";
		}
		try {
			Long s=second.longValue();
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeInMillis(s);
			Date date = calendar.getTime();
			SimpleDateFormat format = new SimpleDateFormat(patten);
			String dateString = format.format(date);
			return dateString;
		}catch (Exception e){
		}
		return "";
	}

	/**
	 * 计算两个时间的分钟
	 */
	public static Integer calculateMinuteTime(Long startTime,Long endTime) {
		long dateDiff = startTime - endTime;
		if (dateDiff < 0) {
			return 0;
		} else {
			return (int) (dateDiff / 1000 / 60); // 分钟
		}
	}
}
