import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public final class DateUtils {
private static Log log = LogFactory.getLog(DateUtils.class);
public final static String DATE_PATTERN = "yyyy-MM-dd"; // 日期格式
public final static String TIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; // 时间格式
public final static long ONEDAY_MS = 24 * 60 * 60 * 1000; //一天毫秒数
public final static String MAX_DATE = "9999-12-31"; // 最大日期
public final static String MAX_TIME = "9999-12-31 23:59:59"; // 最大时间
/**
* 得到系统格式化过的日期 参数为日期类型
*/
public static final String getDateStr(Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate != null) {
df = new SimpleDateFormat(DATE_PATTERN);
returnValue = df.format(aDate);
}
return (returnValue);
}
/**
* 得到年月日时分秒格式化过的日期 参数为日期类型
*/
public static final String getTimeStr(Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate != null) {
df = new SimpleDateFormat(TIME_PATTERN);
returnValue = df.format(aDate);
}
return (returnValue);
}
/**
* 得到当年第一天和最后一天
*/
public static Map<String, Date> getStartendDateOfYear(Date date) {
Date startTime = getFirstDayOfYear(date);
Date endTime = getLastDayOfYear(date);
Map<String, Date> dateMap = new HashMap<String, Date>();
dateMap.put("startTime", startTime);
dateMap.put("endTime", endTime);
return dateMap;
}
/**
* 得到当季度第一天和最后一天
*/
public static Map<String, Date> getStartendDateOfQuarter(Date date) {
Date startTime = getFirstDayOfQuarter(date);
Date endTime = getLastDayOfQuarter(date);
Map<String, Date> dateMap = new HashMap<String, Date>();
dateMap.put("startTime", startTime);
dateMap.put("endTime", endTime);
return dateMap;
}
/**
* 得到当月第一天和最后一天
*/
public static Map<String, Date> getStartendDateOfMonth(Date date) {
Date startTime = getFirstDayOfMonth(date);
Date endTime = getLastDayOfMonth(date);
Map<String, Date> dateMap = new HashMap<String, Date>();
dateMap.put("startTime", startTime);
dateMap.put("endTime", endTime);
return dateMap;
}
/**
* date非空返回指定日期的起始日期(yyyy-MM-dd 00:00:00)和结束日期(yyyy-MM-dd 23:59:59)
*/
public static Map<String, Date> getStartendDateOfDay(Date date) {
Date startTime = getDate0Clock(date);
Date endTime = getDate24Clock(date);
Map<String, Date> dateMap = new HashMap<String, Date>();
dateMap.put("startTime", startTime);
dateMap.put("endTime", endTime);
return dateMap;
}
/**
* 得到本年度第一天
*/
public static Date getFirstDayOfYear(Date date) {
Calendar cal = getCalendar(date);
cal.set(Calendar.MONTH, Calendar.JANUARY);
return getFirstDayOfMonth(cal);
}
/**
* 得到本年度最后一天
*/
public static Date getLastDayOfYear(Date date) {
Calendar cal = getCalendar(date);
cal.set(Calendar.MONTH, Calendar.DECEMBER);
return getLastDayOfMonth(cal);
}
/**
* 得到本季度第一天
*/
public static Date getFirstDayOfQuarter(Date date) {
Calendar cal = getCalendar(date);
int mon = cal.get(Calendar.MONTH);
if (mon >= Calendar.JANUARY && mon <= Calendar.MARCH) {
cal.set(Calendar.MONTH, Calendar.JANUARY);
}
if (mon >= Calendar.APRIL && mon <= Calendar.JUNE) {
cal.set(Calendar.MONTH, Calendar.APRIL);
}
if (mon >= Calendar.JULY && mon <= Calendar.AUGUST) {
cal.set(Calendar.MONTH, Calendar.JULY);
}
if (mon >= Calendar.OCTOBER && mon <= Calendar.DECEMBER) {
cal.set(Calendar.MONTH, Calendar.OCTOBER);
}
return getFirstDayOfMonth(cal);
}
/**
* 得到本季度最后一天
*/
public static Date getLastDayOfQuarter(Date date) {
Calendar cal = getCalendar(date);
int mon = cal.get(Calendar.MONTH);
if (mon >= Calendar.JANUARY && mon <= Calendar.MARCH) {
cal.set(Calendar.MONTH, Calendar.MARCH);
}
if (mon >= Calendar.APRIL && mon <= Calendar.JUNE) {
cal.set(Calendar.MONTH, Calendar.JUNE);
}
if (mon >= Calendar.JULY && mon <= Calendar.AUGUST) {
cal.set(Calendar.MONTH, Calendar.AUGUST);
}
if (mon >= Calendar.OCTOBER && mon <= Calendar.DECEMBER) {
cal.set(Calendar.MONTH, Calendar.DECEMBER);
}
return getLastDayOfMonth(cal);
}
/**
* 得到本月第一天
*/
public static Date getFirstDayOfMonth(Date date) {
Calendar cal = getCalendar(date);
return getFirstDayOfMonth(cal);
}
/**
* 得到本月最后一天的日期
*/
public static Date getLastDayOfMonth(Date date) {
Calendar cal = getCalendar(date);
return getLastDayOfMonth(cal);
}
/**
* 得到本月第一天的日期
*/
public static Date getFirstDayOfMonth(Calendar cal) {
int minDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, minDay);
return getDate0Clock(cal.getTime());
}
/**
* 得到本月最后一天的日期
*/
public static Date getLastDayOfMonth(Calendar cal) {
int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, maxDay);
return getDate24Clock(cal.getTime());
}
/**
* 得到本周第一天
*/
public static Date getFirstDayOfWeek(Date date) {
Calendar cal = getCalendar(date);
return getFirstDayOfWeek(cal);
}
/**
* 得到本周最后一天
*
* @param date
* @return
*/
public static Date getLastDayOfWeek(Date date) {
Calendar cal = getCalendar(date);
return getLastDayOfWeek(cal);
}
/**
* 得到本周第一天
*/
public static Date getFirstDayOfWeek(Calendar cal) {
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
return getDate0Clock(cal.getTime());
}
/**
* 得到本周最后一天
*/
public static Date getLastDayOfWeek(Calendar cal) {
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek() + 6);
return getDate24Clock(cal.getTime());
}
/**
* date非空返回指定日期的零时零分零秒(yyyy-MM-dd 00:00:00)
* date为null返回当前日期的零时零分零秒(yyyy-MM-dd 00:00:00)
*/
public static Date getDate0Clock(Date date) {
return getSpecDate(date, 0, 0, 0);
}
/**
* date非空返回指定日期的零时零分零秒(yyyy-MM-dd 23:59:59)
* date为null返回当前日期的零时零分零秒(yyyy-MM-dd 23:59:59)
*/
public static Date getDate24Clock(Date date) {
return getSpecDate(date, 23, 59, 59);
}
/**
* 将字符串转为对应的日期
*/
public static Date convertStringToDate(String strDate) {
Date aDate = null;
try {
if (log.isDebugEnabled()) {
log.debug("converting date with pattern: " + DATE_PATTERN);
}
String pattern = "\\d{4}[-|/]\\d{2}[-|/]\\d{2}[ ]\\d{2}[:]\\d{2}[:]\\d{2}";
if (strDate.matches(pattern)) {
if (strDate.contains("/")) {
aDate = convertStringToDate("yyyy/MM/dd HH:mm:ss", strDate);
} else {
aDate = convertStringToDate(TIME_PATTERN, strDate);
}
} else {
if (strDate.contains("/")) {
aDate = convertStringToDate("yyyy/MM/dd", strDate);
} else {
aDate = convertStringToDate(DATE_PATTERN, strDate);
}
}
} catch (ParseException pe) {
log.error("Could not convert '" + strDate + "' to a date, throwing exception");
pe.printStackTrace();
}
return aDate;
}
/**
* 根据提供日期格式及日期字符串加工日期
*/
public static final Date convertStringToDate(String aMask, String strDate) throws ParseException {
SimpleDateFormat df = null;
Date date = null;
df = new SimpleDateFormat(aMask);
if (log.isDebugEnabled()) {
log.debug("converting '" + strDate + "' to date with mask '" + aMask + "'");
}
try {
date = df.parse(strDate);
} catch (ParseException pe) {
// log.error("ParseException: " + pe);
throw new ParseException(pe.getMessage(), pe.getErrorOffset());
}
return (date);
}
/**
* 将日期转为指定格式的字符串
*/
public static final String getDateTime(String aMask, Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate == null) {
log.error("aDate is null!");
} else {
df = new SimpleDateFormat(aMask);
returnValue = df.format(aDate);
}
return (returnValue);
}
/**
* 将日期转为系统格式的(年月日)日期字符串
*/
public static final String convertDateToString(Date aDate) {
return getDateTime(DATE_PATTERN, aDate);
}
/**
* 将日期转成年月日 时分秒格式的字符串
*/
public static final String convertDateTimeToString(Date aDate) {
return getDateTime(TIME_PATTERN, aDate);
}
/**
* 在指定日期增加天数得到日期
*/
public static Date addDays(Date date, int days) {
return add(date, days, Calendar.DATE);
}
/**
* 在指定日期增加月数得到日期
*/
public static Date addMonths(Date date, int months) {
return add(date, months, Calendar.MONTH);
}
/**
* 在指定日期增加年数得到日期
*/
public static Date addYears(Date date, int years) {
return add(date, years, Calendar.YEAR);
}
/**
* 是否是同一天
*/
public static boolean isTheSameDate(Date date1, Date date2) {
if (date1!=null && date2!=null) {
return (date1.getTime()/ONEDAY_MS == date2.getTime()/ONEDAY_MS);
}
return false;
}
/**
* 获取最近几周内的日期,返回开始日期和结束日期
*/
public static Map<String, Date> getRecentlyWeeks(int week){
Date dateNow = new Date();
Calendar cl = Calendar.getInstance();
cl.setTime(dateNow);
cl.add(Calendar.WEEK_OF_YEAR, -week);
Date dateFrom = cl.getTime();
Map<String, Date> result = new HashMap<String, Date>();
result.put("startDate", dateFrom);
result.put("endDate", dateNow);
return result;
}
private static Calendar getCalendar(Date date) {
Calendar calendar = Calendar.getInstance();
if(date!=null){
calendar.setTime(date);
}
return calendar;
}
/**
* @param amount 需要增加或减少的量
* @param field 需要增加或减少的参数(年/月/日),例如 Calendar.YEAR,Calendar.MONTH
*/
private static Date add(Date date, int amount, int field) {
Calendar calendar = getCalendar(date);
calendar.add(field, amount);
return calendar.getTime();
}
/**
* 将行如 yyyyMMdd的无分隔符的字符串日期,转换成日期
*/
public static Date convertStrNoSymbolToDate(String dateStr){
Date date = null;
String pattern = "\\d{8}";
if(StrUtils.isNotEmpty(dateStr) && dateStr.matches(pattern)){
dateStr = dateStr.substring(0, 4)+"-"+dateStr.substring(4, 6)+"-"+dateStr.substring(6, 8);
date = convertStringToDate(dateStr);
}
return date;
}
}
时间工具类
最新推荐文章于 2022-06-20 17:54:58 发布