/**
* <p>当前时间</p>
* @return
*/
public static Date getCurrDate(){
Calendar cal = Calendar.getInstance();
return cal.getTime();
}
/**
* <p>当前时间戳</p>
* @return
*/
public static TimeStamp getCurrTimeStamp(){
Calendar cal = Calendar.getInstance();
return new TimeStamp(cal.getTimeInMillis());
}
/**
* <p>把参数日期格式化成pattern格式的字符串</p>
* @param date
* @param pattern
* @return
*/
public static String date2String(Date date,String pattern){
return DateFormatUtils.format(date, pattern);
}
/**
* <p>当前日期格式化成pattern格式的字符串</p>
* @param pattern
* @return
*/
public static String date2String(String pattern){
return DateFormatUtils.format(getCurrDate(), pattern);
}
/**
* 根据日期串返回日期对象
* <p>TODO</p>
* @param format 日期格式 如yyyy-MM-dd
* @param date 日期 如"2013-02-02"
* @return
* @throws ParseException
*/
public static Date stringToDate(String format,String date){
DateFormat df = new SimpleDateFormat(format);
Date dateNew = null;
try {
dateNew = df.parse(date);
} catch (ParseException e) {
log.error("日期转换失败", e);
}
return dateNew;
}
/**
* <p>日期加减</p>
* @param field
* @param amount
* @return
*/
public static Date dateAddMinus(int field,int amount){
Calendar cal = Calendar.getInstance();
cal.add(field, amount);
return cal.getTime();
}
/**
*
* <p>日期加减</p>
* @param date
* @param field
* @param amount
* @return
*/
public static Date dateAddMinus(Date date,int field,int amount){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(field, amount);
return cal.getTime();
}
/**
* <p>获取上几年</p>
* @param years 如为1,当年为2015 则返回2015,2014
* @return
*/
public static List<String> getPrevYears(int years){
List<String> list = new ArrayList<String>();
for(int i=0;i<=years;i++){
Date d = dateAddMinus(Calendar.YEAR,-i);
list.add(date2String(d,"yyyy"));
}
return list;
}
public static Date getFailureDate() throws ParseException{
String failureDateString = "2099-01-01";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date failureDate = sdf.parse(failureDateString);
return failureDate;
}
/**
* <p>给指定日期设置时分秒</p>
* @param hmsStr 时分秒字符串
* @param date 指定日期
* @return
* @throws ParseException
*/
public static Date setHMS2Date(String hmsStr,Date date){
String dateStr = EbpDateUtil.date2String(date, "yyyy-MM-dd");
dateStr += " "+hmsStr;
return EbpDateUtil.stringToDate("yyyy-MM-dd hh:mm:ss", dateStr);
}
/**
* <p>给当前日期设置时分秒</p>
* @param hmsStr 时分秒字符串
* @return
* @throws ParseException
*/
public static Date setHMS2Date(String hmsStr){
String dateStr = EbpDateUtil.date2String(getCurrDate(), "yyyyMMdd");
dateStr += " "+hmsStr;
return EbpDateUtil.stringToDate("yyyyMMdd hh:mm:ss", dateStr);
}
/**
* <p>获取间隔一定天数的日期字符串</p>
* @param sourceDate 源或基准日期
* @param apartDay 间隔天数 往前负数,往后正数
* @param pattern 返回字符串格式化表达式
* @return
*/
public static String getApartDate(Date sourceDate,int apartDay,String pattern){
Calendar c = Calendar.getInstance();
Date date=sourceDate;
String _pattern = "yyyy-MM-dd";
if(!"".equalsIgnoreCase(StringFilter.getStringValue(pattern))){
_pattern = pattern;
}
c.setTime(date);
int day=c.get(Calendar.DATE);
c.set(Calendar.DATE,day+apartDay);
String dayAfter=new SimpleDateFormat(_pattern).format(c.getTime());
return dayAfter;
}
/**
* <p>计算两个日期之间相差的天数 </p>
* @param smdate 较小的时间
* @param bdate 较大的时间
* @return 相差天数
* @throws ParseException
*/
public static Integer getDaysBetween(Date smdate,Date bdate)
{
try{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
smdate=sdf.parse(sdf.format(smdate));
bdate=sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
return new Integer(String.valueOf(between_days));
}catch(ParseException e){
log.error("处理两个日期之间相差的天数格式异常", e);
return null;
}
}
/**
*
* <p>获取精确到日,日期</p>
* @param date
* @return
*/
public static Date dateToDayProcess(Date date){
Date dateDay = null;
if(date!=null){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
try {
dateDay = sdf.parse(sdf.format(date));
} catch (ParseException e) {
log.error("日期精确到日,转换异常");
}
}
return dateDay;
}
/**
* 判断是否小于当天日期 是 返回true
* @param str输入的日期
* @return
* @return
*/
public static boolean isPastDate(Date para){
boolean flag = false;
//在日期字符串非空时执行
if (para != null) {
Date paraDate = dateToDayProcess(para);
Date today = dateToDayProcess(new Date());
//调用Date里面的before方法来做判断
flag = paraDate.before(today);
if (flag) {
flag = true;
}else {
flag = false;
}
}else {
flag = true;
}
return flag;
}