/**
* 常用日期处理工具类
*/
public class dateUtils{
/**
* 格式化当前时间
* @param pattern
* @return
*/
public static String format(String pattern){
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.format(new Date());
}
/**
* 格式化指定时间
* @param date
* @param pattern
* @return
*/
public static String format(Date date, String pattern){
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.format(date);
}
/**
* 解释日期
* @param str
* @param pattern
* @return
* @throws Exception
*/
public static Date parse(String str, String pattern) throws Exception{
SimpleDateFormat df = new SimpleDateFormat(pattern);
Date date = df.parse(str);
return date;
}
/**
* 得到当天开始日期
* @return
* @throws Exception
*/
public static Date getDate() throws Exception{
return OspUtils.parse(OspUtils.format("yyyyMMdd"),"yyyyMMdd");
}
/**
* 在指定的日期增加天数
* @param initDate
* @param diff
* @return
*/
public static Date addDay2Date(Date initDate , int diff){
Calendar cal = Calendar.getInstance();
cal.setTime(initDate);
cal.add(Calendar.DATE, diff);
return new Date(cal.getTimeInMillis());
}
/**
* 返回当天起始时间
* @return
*/
public static Date getCurrentDayStart() throws Exception{
String start = format("yyyyMMdd")+"000000";
return parse(start, yyyyMMddhhmmss);
}
/**
* 返回当天结束时间
* @return
*/
public static Date getCurrentDayend() throws Exception{
String start = format("yyyyMMdd")+"235959";
return parse(start, yyyyMMddhhmmss);
}
/**
* 判断日期是否是当天时间
* @param date
* @return
*/
public static boolean judgeDateIsCurrentDay(Date date){
try {
long time = date.getTime();
long Daystart = getCurrentDayStart().getTime();
long Dayend = getCurrentDayend().getTime();
return Daystart<=time && time<=Dayend;
} catch (Exception e) {
}
return false;
}
/**
* 比较日期字符串返回天数
* @param strBegin 格式:yyyy-MM-dd
* @param strEnd 格式:yyyy-MM-dd
* @return
* @throws Exception
*/
public static int getDifferDays(String strBegin,String strEnd) throws Exception {
java.util.Calendar calst = java.util.Calendar.getInstance();
java.util.Calendar caled = java.util.Calendar.getInstance();
calst.setTime(parse(strBegin.concat(" 00:00:00"), yyyyMMddhhmmss));
caled.setTime(parse(strEnd.concat(" 00:00:00"), yyyyMMddhhmmss));
//得到两个日期相差的天数
int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst
.getTime().getTime() / 1000)) / 3600 / 24;
return days;
}
/**
* 比较日期返回天数
* @param strBegin
* @param strEnd
* @return
*/
public static final int getDifferDays(Date strBegin, Date strEnd) {
java.util.Calendar calst = java.util.Calendar.getInstance();
java.util.Calendar caled = java.util.Calendar.getInstance();
calst.setTime(strBegin);
caled.setTime(strEnd);
//设置时间为0时
calst.set(java.util.Calendar.HOUR_OF_DAY, 0);
calst.set(java.util.Calendar.MINUTE, 0);
calst.set(java.util.Calendar.SECOND, 0);
caled.set(java.util.Calendar.HOUR_OF_DAY, 0);
caled.set(java.util.Calendar.MINUTE, 0);
caled.set(java.util.Calendar.SECOND, 0);
//得到两个日期相差的天数
int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst
.getTime().getTime() / 1000)) / 3600 / 24;
return days;
}
public static final String DATE_PATTERN_YYYY_MM_DD = "yyyy-MM-dd";
public static final String yyyyMMddhhmmss = "yyyyMMddhhmmss";
}