DateHelper


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DateUtil {

	/**
	 * 字符串转日期类型
	 * 
	 * @author HeCheng
	 * @time 2010-12-08 18:31:46
	 * @return
	 */
	public static Date convertStringToDate(String time, int type) {
		String format = "";
		if (type == 1) {
			format = "yyyy-MM-dd";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(time);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 取近一年
	 * 
	 * @author HeCheng
	 * @time 2010-12-08 21:27:41
	 * @return
	 */
	public static String getLastOneYearDay() {
		return getLastNumberDayBeforeYesterDay(365);
	}
	
	/**
	 * 取近三个月
	 * 
	 * @author HeCheng
	 * @time 2010-12-08 21:27:41
	 * @return
	 */
	public static String getLastThreeMonthDay() {
		return getLastNumberDayBeforeYesterDay(90);
	}
	
	/**
	 * 取近一个月
	 * 
	 * @author HeCheng
	 * @time 2010-12-08 21:27:41
	 * @return
	 */
	public static String getLastMonthDay() {
		return getLastNumberDayBeforeYesterDay(30);
	}
	
	/**
	 * 取近一周
	 * 
	 * @author HeCheng
	 * @time 2010-12-08 21:27:41
	 * @return
	 */
	public static String getLastWeekDay() {
		return getLastNumberDayBeforeYesterDay(7);
	}

	/**
	 * 取昨天的前XX天
	 * 
	 * @author HeCheng
	 * @time 2010-12-08 21:06:08
	 * @param number
	 * @return
	 */
	public static String getLastNumberDayBeforeYesterDay(int number) {
		String yesterDay = getYesterdayOrTomorrow(getNowDate(), -1);
		return getLastNumberDay(yesterDay, number);
	}

	/**
	 * 取之前XX天
	 * 
	 * @author HeCheng
	 * @time 2010-12-08 21:06:08
	 */
	@SuppressWarnings("deprecation")
	public static String getLastNumberDay(String day, int number) {
		String ntime = "";
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date startDate = df.parse(day);
			Date endDate = null;
			endDate = new Date(startDate.getTime()
					- (((long) number * (long) 24 * (long) 3600 * (long) 1000)));
			ntime = endDate.getYear() + 1900 + "-" + (endDate.getMonth() + 1)
					+ "-" + endDate.getDate();
		} catch (Exception e) {
			System.out.println(e);
		}
		return ntime;
	}

	/**
	 * 取上个月的昨天
	 * 
	 * @author HeCheng
	 * @time 2010-12-08 18:41:14
	 * @param yesterday
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String getYesterdayOnLastMonth(String yesterday) {
		String ntime = "";
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date startDate = df.parse(yesterday);
			Date endDate = null;
			endDate = new Date(startDate.getTime()
					- (((long) 30 * (long) 24 * (long) 3600 * (long) 1000)));
			ntime = endDate.getYear() + 1900 + "-" + (endDate.getMonth() + 1)
					+ "-" + endDate.getDate();
		} catch (Exception e) {
			System.out.println(e);
		}
		return ntime;
	}

	/**
	 * 取今年第一天
	 * 
	 * @author HeCheng
	 * @time 2010-12-08 18:44:12
	 * @param nowDate
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String getFirstDayInYear(String nowDate) {
		String ntime = "";
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date startDate = df.parse(nowDate);
			ntime = startDate.getYear() + 1900 + "-01-01";
		} catch (Exception e) {
			System.out.println(e);
		}
		return ntime;
	}

	/**
	 * 取本季度第一天
	 * 
	 * @author HeCheng
	 * @time 2010-12-08 18:46:33
	 * @param nowDate
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String getFirstDayInThisQuarter(String nowDate) {
		String ntime = "";
		int nowMonth;
		int nowYear;
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date startDate = df.parse(nowDate);
			nowMonth = startDate.getMonth() + 1;
			nowYear = startDate.getYear() + 1900;
			while (true) {
				if (nowMonth >= 10) {
					ntime = nowYear + "-10-01";
					break;
				}
				if (nowMonth >= 7) {
					ntime = nowYear + "-07-01";
					break;
				}
				if (nowMonth >= 4) {
					ntime = nowYear + "-04-01";
					break;
				}
				if (nowMonth >= 1) {
					ntime = nowYear + "-01-01";
					break;
				}
			}
		} catch (Exception e) {
			System.out.println(e);
		}
		return ntime;
	}

	/**
	 * 取昨天
	 * 
	 * @author HeCheng
	 * @time 2010-12-08 20:27:21
	 * @return
	 */
	public static String getYesterDay() {
		return getYesterdayOrTomorrow(getNowDate(), -1);
	}

	/**
	 * 取明天
	 * 
	 * @author HeCheng
	 * @time 2010-12-08 20:27:21
	 * @return
	 */
	public static String getTomorrow() {
		return getYesterdayOrTomorrow(getNowDate(), 1);
	}

	/**
	 * 取今天
	 * 
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String getNowDate() {
		Date date = new Date();
		int nowMonth = date.getMonth() + 1;
		int nowYear = date.getYear() + 1900;
		int day = date.getDate();
		String startTime = nowYear + "-" + nowMonth + "-" + day;
		return startTime;
	}

	/**
	 * 取时间段
	 * 
	 * @author HeCheng
	 * @time 2010-12-08 18:34:22
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static List<String> getTimes(String startTime, String endTime) {
		List<String> dayList = new ArrayList<String>();
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date startDate = df.parse(startTime);
			Date endDate = df.parse(endTime);
			String now = "";
			for (long i = startDate.getTime(); i <= endDate.getTime(); i += (long) 24
					* (long) 3600 * (long) 1000) {
				Date date = new Date(i);
				now = date.getYear() + 1900 + "-" + (date.getMonth() + 1) + "-"
						+ date.getDate();
				dayList.add(now);
			}
		} catch (Exception e) {
			System.out.println(e);
		}
		return dayList;
	}

	/**
	 * 取当前月第一天
	 * 
	 * @author HeCheng
	 * @time 2010-12-08 18:34:29
	 * 
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String getMonthFirstDay() {
		Date date = new Date();
		int nowMonth = date.getMonth() + 1;
		int nowYear = date.getYear() + 1900;
		String startTime = nowYear + "-" + nowMonth + "-1";
		return startTime;
	}

	/**
	 * 取明天或昨天
	 * 
	 * @param nowDate
	 * @param con
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String getYesterdayOrTomorrow(String nowDate, int con) {
		String ntime = "";
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date startDate = df.parse(nowDate);
			Date endDate = null;
			if (con == -1) {
				endDate = new Date(startDate.getTime() - (long) 24
						* (long) 3600 * (long) 1000);
			} else {
				endDate = new Date(startDate.getTime() + (long) 24
						* (long) 3600 * (long) 1000);
			}
			ntime = endDate.getYear() + 1900 + "-" + (endDate.getMonth() + 1)
					+ "-" + endDate.getDate();
		} catch (Exception e) {
			System.out.println(e);
		}
		return ntime;
	}

	/**
	 * 取月的最后一天
	 * 
	 * @param time
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String getMonthEndDay(String time) {
		String ntime = "";
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date startDate = df.parse(time);
			int nowMonth = startDate.getMonth() + 1;
			int nextMonth = nowMonth + 1;
			int nowYear = startDate.getYear() + 1900;
			String nextTime = nowYear + "-" + nextMonth + "-1";
			Date tmpDate = df.parse(nextTime);
			Date endDate = new Date(tmpDate.getTime() - 24 * 3600 * 1000);
			ntime = endDate.getYear() + 1900 + "-" + (endDate.getMonth() + 1)
					+ "-" + endDate.getDate();
		} catch (Exception e) {
			System.out.println(e);
		}
		return ntime;
	}
}

你可能感兴趣的:(Date)