日历1 extends 日历 - s9797456/MyCommonCodeSnippets GitHub Wiki

package com.xinle.car.admin.common.util;

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

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

public class DateUtil extends DateUtils {

public static final String YYYY = "yyyy";
public static final String YYYY_MM_DD = "yyyy-MM-dd";
public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public static final String YYYYMMDD = "yyyyMMdd";
public static final String HH_MM = "HH:mm";

/**
 * 判断当前日期是星期几
 * 
 * @param pTime
 *            修要判断的时间
 * @return dayForWeek 判断结果
 * @Exception 发生异常
 */
public static int dayForWeek(String pTime) throws Exception {
	SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
	Calendar c = Calendar.getInstance();
	c.setTime(format.parse(pTime));
	int dayForWeek = 0;
	if (c.get(Calendar.DAY_OF_WEEK) == 1) {
		dayForWeek = 7;
	} else {
		dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
	}
	return dayForWeek;
}
	
/**
 * Get current date(java.util.Date)
 * 
 * @return
 */
public static Date getCurrentDate() {
	Calendar cal = Calendar.getInstance();
	cal.set(Calendar.HOUR_OF_DAY, 0);
	cal.set(Calendar.MINUTE, 0);
	cal.set(Calendar.SECOND, 0);
	cal.set(Calendar.MILLISECOND, 0);
	return cal.getTime();
}

public static Date getCurrentDateHM() {
	Calendar cal = Calendar.getInstance();
	cal.set(Calendar.SECOND, 0);
	cal.set(Calendar.MILLISECOND, 0);
	return cal.getTime();
}

/**
 * Get current date(java.util.Date)
 * 
 * @return
 */
public static String getCurrentDateAsStr() {
	return parseDate(new Date(), YYYY_MM_DD_HH_MM_SS);
}

public static String getCurrentDateAsStr(String formatStr) {
	return parseDate(new Date(), formatStr);
}

/**
 * Parses Date object to formatted string
 * 
 * @param date
 *            date to be converted
 * @param formatStr
 *            Date/Time pattern. Example: ddMMyyyy or HHmmss or any other
 *            patterns
 * @return String in required format Format : dd = Day MM = Month yyyy = Year HH
 *         = Hour mm = Minute ss = Second All format same as SimpleDateFormat.
 *         Null is returned if the date object is null.
 * @since 22/03/2001
 */
public static String parseDate(Date date, String formatStr) {
	if (date != null) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(formatStr);
		return dateFormat.format(date);
	}
	return null;
}

/**
 * Parses Date object to formatted string
 * 
 * @param date
 *            date to be converted
 * @return String in required format Format : dd = Day MM = Month yyyy = Year HH
 *         = Hour mm = Minute ss = Second All format same as SimpleDateFormat.
 *         Null is returned if the date object is null.
 */
public static String parseDate(Date date) {
	if (date != null) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(YYYY_MM_DD);
		return dateFormat.format(date);
	}
	return null;
}

/**
 * Parses Date object to formatted string
 * 
 * @param date
 *            date to be converted
 * @return String in required format Format : dd = Day MM = Month yyyy = Year HH
 *         = Hour mm = Minute ss = Second All format same as SimpleDateFormat.
 *         Null is returned if the date object is null.
 */
public static String parseTime(Date date) {
	if (date != null) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(HH_MM);
		return dateFormat.format(date);
	}
	return null;
}

/**
 * Returns the current timestamp.
 * 
 * @return Timestamp
 */
public static Timestamp getSystemTimestamp() {
	return new Timestamp(System.currentTimeMillis());
}

public static Date parseDate(String date, String format) {
	try {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		if (date != null) {
			return sdf.parse(date);
		} else {
			return null;
		}
	} catch (ParseException e) {
		return null;
	}
}

public static Date parseDate(String date) {
	try {
		SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
		if (date != null) {
			return sdf.parse(date);
		} else {
			return null;
		}
	} catch (ParseException e) {
		return null;
	}
}

public static String getCurrentTime() {
	return String.valueOf(System.currentTimeMillis());
}

public static Date addDate(Date date, int days) {
	if (null == date) {
		return null;
	}
	Calendar cal = Calendar.getInstance();
	cal.setTime(date);
	cal.add(Calendar.DAY_OF_MONTH, days);
	return cal.getTime();
}

public static Date addMonth(Date date, int months) {
	if (null == date) {
		return null;
	}
	Calendar cal = Calendar.getInstance();
	cal.setTime(date);
	cal.add(Calendar.MONTH, months);
	return cal.getTime();
}

public static Date subDate(Date date, int days) {
	if (null == date) {
		return null;
	}
	Calendar cal = Calendar.getInstance();
	cal.setTime(date);
	cal.add(Calendar.DAY_OF_MONTH, -days);
	return cal.getTime();
}

public static Date subMonth(Date date, int months) {
	if (null == date) {
		return null;
	}
	Calendar cal = Calendar.getInstance();
	cal.setTime(date);
	cal.add(Calendar.MONTH, -months);
	return cal.getTime();
}

public static int getField(Date date, int field) {
	if (null == date) {
		return -1;
	}
	Calendar cal = Calendar.getInstance();
	cal.setTime(date);

	return cal.get(field);
}

 /**
 * 这个月有几天
 * @param date
 * @return
 */
public static int getDaysOfMonth(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    int temp = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    return temp;
}

}