DateUtil.java 8.7 KB
package cn.com.polysoft.utils;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 
 *
 *	日期转换处理
 */
public class DateUtil {
	
	public static final String C_DATE_DIVISION = "-";
	public static final String C_TIME_PATTON_DEFAULT = "yyyy-MM-dd HH:mm:ss";
	public static final String C_DATE_PATTON_DEFAULT = "yyyy-MM-dd";
	public static final String C_TIMES_PATTON_DEFAULT = "HH:mm:ss";
	

	/**
	 * 返回当前时间
	 * @return
	 */
	public static Date getCurrentDate() {
		Calendar cal = Calendar.getInstance();
		Date currDate = cal.getTime();
		return currDate;
	}

	/**
	 * 返回当前日期(String)
	 * @return
	 */
	public static String getCurrentDateStr() {
		Calendar cal = Calendar.getInstance();
		Date currDate = cal.getTime();
		return format(currDate,C_DATE_PATTON_DEFAULT);
	}
	
	/**
	 * 返回当前时间(String)
	 * @return
	 */
	public static String getCurrentTimeStr() {
		Calendar cal = Calendar.getInstance();
		Date currDate = cal.getTime();
		return format(currDate,C_TIMES_PATTON_DEFAULT);
	}

	/**
	 * 格式化日期字符串
	 * @param dateValue
	 * @return
	 */
	public static Date parseDate(String dateValue) {
		return parseDate(C_DATE_PATTON_DEFAULT, dateValue);
	}
	/**
	 * 格式化时间字符串
	 * @param dateValue
	 * @return
	 */
	public static Date parseDateTime(String dateValue) {
		return parseDate(C_TIME_PATTON_DEFAULT, dateValue);
	}

	/**
	 * 时间格式化
	 * @param strFormat
	 * @param dateValue
	 * @return
	 */
	public static Date parseDate(String strFormat, String dateValue) {
		if (dateValue == null)
			return null;

		if (strFormat == null)
			strFormat = C_TIME_PATTON_DEFAULT;

		SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
		Date newDate = null;

		try {
			newDate = dateFormat.parse(dateValue);
		} catch (ParseException pe) {
			newDate = null;
		}

		return newDate;
	}

	/**
	 * 将Timestamp类型的日期转换为系统参数定义的格式的字符串(yyyy-MM-dd)
	 * 
	 * @param timestamp 需要转换的日期。
	 * @return 转换后符合给定格式的日期字符串
	 */
	public static String format(Date timestamp) {
		return format(timestamp, C_DATE_PATTON_DEFAULT);
	}
	
	/**
	 * 将Timestamp类型的日期转换为系统参数定义的格式的字符串(yyyy-MM-dd HH:mm:ss)
	 * 
	 * @param timestamp   需要转换的日期。
	 * @return 转换后符合给定格式的日期字符串
	 */
	public static String formatTime(Date timestamp) {
		return format(timestamp, C_TIME_PATTON_DEFAULT);
	}

	/**
	 * 将Date类型的日期转换为系统参数定义的格式的字符串。
	 * 
	 * @param timestamp
	 * @param pattern
	 * @return
	 */
	public static String format(Date timestamp, String pattern) {
		if (timestamp == null || pattern == null)
			return null;

		SimpleDateFormat dateFromat = new SimpleDateFormat();
		dateFromat.applyPattern(pattern);

		return dateFromat.format(timestamp);
	}

	/**
	 * 取得格式化时间字符串(指定format)
	 * @param aTs_Datetime
	 * @param as_Format
	 * @return
	 */
	public static String formatTime(Date aTs_Datetime, String as_Format) {
		if (aTs_Datetime == null || as_Format == null)
			return null;

		SimpleDateFormat dateFromat = new SimpleDateFormat();
		dateFromat.applyPattern(as_Format);

		return dateFromat.format(aTs_Datetime);
	}
	
	/**
	 * 取得格式化时间字符串(指定pattern)
	 * @return
	 */
	public static String format(Timestamp dateTime, String pattern) {
		if (dateTime == null || pattern == null)
			return null;

		SimpleDateFormat dateFromat = new SimpleDateFormat();
		dateFromat.applyPattern(pattern);

		return dateFromat.format(dateTime);
	}

	/**
	 * 取得指定日期N天后的日期
	 * 
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date addDays(Date date, int days) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		cal.add(Calendar.DAY_OF_MONTH, days);

		return cal.getTime();
	}
	/**
	 * 取得指定日期N天前的日期
	 * 
	 * @param days
	 * @return
	 */
	public static String beforeDays(String dateString, int days) {
		String dateBefore = "";
		Integer day = Integer.parseInt(dateString.substring(8));
		if(day > days){
			 dateBefore= dateString.substring(0,4) + C_DATE_DIVISION +dateString.substring(5,7) +C_DATE_DIVISION+ (day - days);
		}else{
		     dateBefore = dateString.substring(0,4) + C_DATE_DIVISION + (Integer.parseInt(dateString.substring(5,7))-1)+ C_DATE_DIVISION + day;
		}
		return dateBefore;
	}
	/**
	 * 计算两个日期之间相差的天数
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int daysBetween(Date date1, Date date2) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date1);
		long time1 = cal.getTimeInMillis();
		cal.setTime(date2);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 计算当前日期相对于"1977-12-01"的天数
	 * 
	 * @param date
	 * @return
	 */
	public static long getRelativeDays(Date date) {
		Date relativeDate = DateUtil.parseDate("yyyy-MM-dd", "1977-12-01");

		return DateUtil.daysBetween(relativeDate, date);
	}

	/**
	 * 传入时间字符串,加一天后返回Date
	 * 
	 * @param date	时间 格式 YYYY-MM-DD
	 * @return
	 */
	public static Date addDate(String date) {
		if (date == null) {
			return null;
		}

		Date tempDate = parseDate(C_DATE_PATTON_DEFAULT, date);
		String year = format(tempDate, "yyyy");
		String month = format(tempDate, "MM");
		String day = format(tempDate, "dd");

		GregorianCalendar calendar = new GregorianCalendar(Integer
				.parseInt(year), Integer.parseInt(month) - 1, Integer
				.parseInt(day));

		calendar.add(Calendar.DATE, 1);
		return calendar.getTime();
	}
	/**
	 * 取得12月前的日期
	 * @return
	 */
	public static Date getDateBeforTwelveMonth() {
		String date = "";
		Calendar cla = Calendar.getInstance();
		cla.setTime(getCurrentDate());
		int year = cla.get(Calendar.YEAR) - 1;
		int month = cla.get(Calendar.MONTH) + 1;
		if (month > 9) {
			date = String.valueOf(year) + C_DATE_DIVISION
					+ String.valueOf(month) + C_DATE_DIVISION + "01";
		} else {
			date = String.valueOf(year) + C_DATE_DIVISION + "0"
					+ String.valueOf(month) + C_DATE_DIVISION + "01";
		}
		Date dateBefore = parseDate(date);
		return dateBefore;
	}
	/**
	 * 取得指定日期所在周开始
	 * @param date
	 * @return
	 */
	public static String getWeekStartDate(String date){
        Calendar cal =Calendar.getInstance();
        if (date!=null) {
			cal.setTime(DateUtil.parseDate(date));
		}
		cal.add(Calendar.WEEK_OF_YEAR, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String theWeekStartDate = format(cal.getTime());
        cal.clear();
        return theWeekStartDate;
	}
	/**
	 * 取得指定日期所在周结束
	 * @param date
	 * @return
	 */
	public static String getWeekEndDate(String date){
        Calendar cal =Calendar.getInstance();
        if (date!=null) {
			cal.setTime(DateUtil.parseDate(date));
		}
		cal.add(Calendar.WEEK_OF_YEAR, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        String theWeekStartDate = format(cal.getTime());
        cal.clear();
        return theWeekStartDate;
	}
	/**
	 * 取得指定日期所在月开始
	 * @param date
	 * @return
	 */
	public static String getMonthStartDate(String date){
        Calendar cal =Calendar.getInstance();
        if (date!=null&&date.trim().length()!=0) {
			cal.setTime(DateUtil.parseDate(date));
		}
		cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH,1 );
        String theWeekStartDate = format(cal.getTime(), C_DATE_PATTON_DEFAULT);
        
        cal.clear();
        return theWeekStartDate;
	}
	/**
	 * 取得指定日期所在月开始
	 * @param date
	 * @return
	 */
	public static String getMonthEndDate(String date){
        Calendar cal =Calendar.getInstance();
        if (date!=null&&date.trim().length()!=0) {
			cal.setTime(DateUtil.parseDate(date));
		}
		cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String theWeekStartDate = format(cal.getTime(), C_DATE_PATTON_DEFAULT);
        cal.clear();
        return theWeekStartDate;
	}
	
	/**
	 * 得到两个日期之间相差的年数
	 * 
	 * @param date1
	 * @param date2
	 * @author mowei
	 */
	public static int getDifferYear(Date date1, Date date2) {
		Calendar c1 = Calendar.getInstance();   
		Calendar c2 = Calendar.getInstance(); 
		c1.setTime(date1);   
		c2.setTime(date2);   
		return c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
	}
	
	public static void main(String[]args){
		System.out.println(getDifferYear(new Date(),parseDate("2000-12-31")));
	}
	
}