importjava.text.DateFormat;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;importjava.util.GregorianCalendar;importjava.util.Locale;importjava.util.logging.Level;importjava.util.logging.Logger;public classDateUtil {public final static long ONE_DAY_SECONDS = 86400;public final static String shortFormat = "yyyyMMdd";public final static String longFormat = "yyyyMMddHHmmss";public final static String concurrentFormat = "yyyyMMddHHmmssSSS";public final static String shortConcurrentFormat = "yyMMddHHmmssSSS";public final static String webFormat = "yyyy-MM-dd";public final static String webMonthFormat = "yyyy-MM";public final static String timeFormat = "HH:mm:ss";public final static String monthFormat = "yyyyMM";public final static String chineseDtFormat = "yyyy年MM月dd日";public final static String chineseYMFormat = "yyyy年MM月";public final static String newFormat = "yyyy-MM-dd HH:mm:ss";public final static String noSecondFormat = "yyyy-MM-dd HH:mm";public final static String MdFormat = "MM-dd";public final static long ONE_DAY_MILL_SECONDS = 86400000;public staticDateFormat getNewDateFormat(String pattern) {
DateFormat df= newSimpleDateFormat(pattern);
df.setLenient(false);returndf;
}public staticString format(Date date, String format) {if (date == null) {return null;
}return newSimpleDateFormat(format).format(date);
}public staticString format(String dateStr, String oldFormat, String newFormat) {if (dateStr == null) {return null;
}
String result= null;
DateFormat oldDateFormat= newSimpleDateFormat(oldFormat);
DateFormat newDateFormat= newSimpleDateFormat(newFormat);try{
Date date=oldDateFormat.parse(dateStr);
result=newDateFormat.format(date);
}catch(ParseException e) {
e.printStackTrace();
}returnresult;
}public staticDate parseDateNoTime(String sDate) {
DateFormat dateFormat= newSimpleDateFormat(shortFormat);
Date date= null;if ((sDate == null) || (sDate.length()
}if (!StringUtil.isNumeric(sDate)) {return null;
}try{
date=dateFormat.parse(sDate);
}catch(ParseException e) {
e.printStackTrace();
}returndate;
}public static Date parseDateNoTime(String sDate, String format) throwsParseException {if(StringUtil.isBlank(format)) {throw new ParseException("Null format. ", 0);
}
DateFormat dateFormat= newSimpleDateFormat(format);if ((sDate == null) || (sDate.length()
}returndateFormat.parse(sDate);
}public staticDate parseDateNoTimeWithDelimit(String sDate, String delimit)throwsParseException {
sDate= sDate.replaceAll(delimit, "");
DateFormat dateFormat= newSimpleDateFormat(shortFormat);if ((sDate == null) || (sDate.length() !=shortFormat.length())) {throw new ParseException("length not match", 0);
}returndateFormat.parse(sDate);
}public staticDate parseDateLongFormat(String sDate) {
DateFormat dateFormat= newSimpleDateFormat(longFormat);
Date d= null;if ((sDate != null) && (sDate.length() ==longFormat.length())) {try{
d=dateFormat.parse(sDate);
}catch(ParseException ex) {return null;
}
}returnd;
}public staticDate parseDateNewFormat(String sDate) {
DateFormat dateFormat= newSimpleDateFormat(newFormat);
Date d= null;
dateFormat.setLenient(false);if ((sDate != null) && (sDate.length() ==newFormat.length())) {try{
d=dateFormat.parse(sDate);
}catch(ParseException ex) {return null;
}
}returnd;
}public staticDate parseDateNoSecondFormat(String sDate) {
DateFormat dateFormat= newSimpleDateFormat(noSecondFormat);
Date d= null;
dateFormat.setLenient(false);if ((sDate != null) && (sDate.length() ==noSecondFormat.length())) {try{
d=dateFormat.parse(sDate);
}catch(ParseException ex) {return null;
}
}returnd;
}public staticDate parseDateWebFormat(String sDate) {
DateFormat dateFormat= newSimpleDateFormat(webFormat);
Date d= null;
dateFormat.setLenient(false);if ((sDate != null) && (sDate.length() ==webFormat.length())) {try{
d=dateFormat.parse(sDate);
}catch(ParseException ex) {return null;
}
}returnd;
}public staticDate parseDateWebMonthFormat(String sDate) {
DateFormat dateFormat= newSimpleDateFormat(webMonthFormat);
Date d= null;
dateFormat.setLenient(false);if ((sDate != null) && (sDate.length() ==webMonthFormat.length())) {try{
d=dateFormat.parse(sDate);
}catch(ParseException ex) {return null;
}
}returnd;
}/*** 计算当前时间几小时之后的时间
*
*@paramdate
*@paramhours
*
*@return
*/
public static Date addHours(Date date, longhours) {return addMinutes(date, hours * 60);
}/*** 计算当前时间几分钟之后的时间
*
*@paramdate
*@paramminutes
*
*@return
*/
public static Date addMinutes(Date date, longminutes) {return addSeconds(date, minutes * 60);
}/***@paramdate1
*@paramsecs
*
*@return
*/
public static Date addSeconds(Date date1, longsecs) {return new Date(date1.getTime() + (secs * 1000));
}/*** 判断输入的字符串是否为合法的小时
*
*@paramhourStr
*
*@returntrue/false*/
public static booleanisValidHour(String hourStr) {if (!StringUtil.isEmpty(hourStr) &&StringUtil.isNumeric(hourStr)) {int hour = newInteger(hourStr).intValue();if ((hour >= 0) && (hour <= 23)) {return true;
}
}return false;
}/*** 判断输入的字符串是否为合法的分或秒
*
*@paramminuteStr
*
*@returntrue/false*/
public static booleanisValidMinuteOrSecond(String str) {if (!StringUtil.isEmpty(str) &&StringUtil.isNumeric(str)) {int hour = newInteger(str).intValue();if ((hour >= 0) && (hour <= 59)) {return true;
}
}return false;
}/*** 取得新的日期
*
*@paramdate1
* 日期
*@paramdays
* 天数
*
*@return新的日期*/
public static Date addDays(Date date1, longdays) {
Calendar cal=Calendar.getInstance();
cal.setTime(date1);
cal.add(Calendar.DATE, (int) days);returncal.getTime();
}public static String getTomorrowDateString(String sDate) throwsParseException {
Date aDate=parseDateNoTime(sDate);
aDate=addSeconds(aDate, ONE_DAY_SECONDS);returngetDateString(aDate);
}public static String getTomorrowDateNewFMTString(String sDate) throwsParseException {
Date aDate=parseDateWebFormat(sDate);
aDate= addDays(aDate, 1);returngetWebDateString(aDate);
}public static String getTomorrowDateNewFormatString(String sDate) throwsParseException {
Date aDate=parseDateNewFormat(sDate);
aDate= addDays(aDate, 1);returngetWebDateString(aDate);
}public staticString getLongDateString(Date date) {
DateFormat dateFormat= newSimpleDateFormat(longFormat);returngetDateString(date, dateFormat);
}public staticString getNewFormatDateString(Date date) {
DateFormat dateFormat= newSimpleDateFormat(newFormat);returngetDateString(date, dateFormat);
}public staticString getWebFormatDateString(Date date) {
DateFormat dateFormat= newSimpleDateFormat(webFormat);returngetDateString(date, dateFormat);
}public staticString getConcurrentFormatDateString(Date date) {
DateFormat dateFormat= newSimpleDateFormat(concurrentFormat);returngetDateString(date, dateFormat);
}public staticString getDateString(Date date, DateFormat dateFormat) {if (date == null || dateFormat == null) {return null;
}returndateFormat.format(date);
}public static String getYesterDayDateString(String sDate) throwsParseException {
Date aDate=parseDateNoTime(sDate);
aDate= addSeconds(aDate, -ONE_DAY_SECONDS);returngetDateString(aDate);
}/***@return当天的时间格式化为"yyyyMMdd"*/
public staticString getDateString(Date date) {
DateFormat df=getNewDateFormat(shortFormat);returndf.format(date);
}public staticString getWebDateString(Date date) {
DateFormat dateFormat=getNewDateFormat(webFormat);returngetDateString(date, dateFormat);
}/*** 取得“X年X月X日”的日期格式
*
*@paramdate
*
*@return
*/
public staticString getChineseDateString(Date date) {
DateFormat dateFormat=getNewDateFormat(chineseDtFormat);returngetDateString(date, dateFormat);
}public staticString getTodayString() {
DateFormat dateFormat=getNewDateFormat(shortFormat);return getDateString(newDate(), dateFormat);
}public staticString getTomorrowString() {
DateFormat dateFormat=getNewDateFormat(shortFormat);return getDateString(DateUtil.addDays(new Date(), 1), dateFormat);
}public staticString getTimeString(Date date) {
DateFormat dateFormat=getNewDateFormat(timeFormat);returngetDateString(date, dateFormat);
}public static String getBeforeDayString(intdays) {
Date date= new Date(System.currentTimeMillis() - (ONE_DAY_MILL_SECONDS *days));
DateFormat dateFormat=getNewDateFormat(shortFormat);returngetDateString(date, dateFormat);
}/*** 取得两个日期间隔毫秒数(日期1-日期2)
*
*@paramone
* 日期1
*@paramtwo
* 日期2
*
*@return间隔秒数*/
public static longgetDiffMillis(Date one, Date two) {
Calendar sysDate= newGregorianCalendar();
sysDate.setTime(one);
Calendar failDate= newGregorianCalendar();
failDate.setTime(two);return (sysDate.getTimeInMillis() -failDate.getTimeInMillis());
}/*** 取得两个日期间隔秒数(日期1-日期2)
*
*@paramone
* 日期1
*@paramtwo
* 日期2
*
*@return间隔秒数*/
public static longgetDiffSeconds(Date one, Date two) {
Calendar sysDate= newGregorianCalendar();
sysDate.setTime(one);
Calendar failDate= newGregorianCalendar();
failDate.setTime(two);return (sysDate.getTimeInMillis() - failDate.getTimeInMillis()) / 1000;
}/*** 取得两个日期间隔分钟数(日期1-日期2)
*
*@paramone
* 日期1
*@paramtwo
* 日期2
*
*@return间隔秒数*/
public static longgetDiffMinutes(Date one, Date two) {
Calendar sysDate= newGregorianCalendar();
sysDate.setTime(one);
Calendar failDate= newGregorianCalendar();
failDate.setTime(two);return (sysDate.getTimeInMillis() - failDate.getTimeInMillis()) / (60 * 1000);
}/*** 取得两个日期的间隔天数
*
*@paramone
*@paramtwo
*
*@return间隔天数*/
public static longgetDiffDays(Date one, Date two) {
Calendar sysDate= newGregorianCalendar();
sysDate.setTime(one);
Calendar failDate= newGregorianCalendar();
failDate.setTime(two);return (sysDate.getTimeInMillis() - failDate.getTimeInMillis()) / (24 * 60 * 60 * 1000);
}/*** 取得两个日期相差的自然日
*
*@paramdate1
*@paramdate2
*@return
*/
public static longgetDiffNaturalDays(Date date1, Date date2) {
Long diffDays= 0L;
DateFormat dateFormat= newSimpleDateFormat(webFormat);//去掉时分秒
String dateStr1 =dateFormat.format(date1);
String dateStr2=dateFormat.format(date2);try{
diffDays= (dateFormat.parse(dateStr1).getTime() - dateFormat.parse(dateStr2).getTime()) / (24 * 60 * 60 * 1000);
}catch(ParseException e) {
e.printStackTrace();
}returnMath.abs(diffDays);
}/*** 取得两个日期相差的自然日
*
*@paramdate1
*@paramdate2
*@return
*/
public static longgetDiffNaturalDayNotAbs(Date date1, Date date2) {
Long diffDays= 0L;
DateFormat dateFormat= newSimpleDateFormat(webFormat);//去掉时分秒
String dateStr1 =dateFormat.format(date1);
String dateStr2=dateFormat.format(date2);try{
diffDays= (dateFormat.parse(dateStr1).getTime() - dateFormat.parse(dateStr2).getTime()) / (24 * 60 * 60 * 1000);
}catch(ParseException e) {
e.printStackTrace();
}returndiffDays;
}public static String getBeforeDayString(String dateString, intdays) {
Date date;
DateFormat df=getNewDateFormat(shortFormat);try{
date=df.parse(dateString);
}catch(ParseException e) {
date= newDate();
}
date= new Date(date.getTime() - (ONE_DAY_MILL_SECONDS *days));returndf.format(date);
}public static booleanisValidShortDateFormat(String strDate) {if (strDate.length() !=shortFormat.length()) {return false;
}try{
Integer.parseInt(strDate);//---- 避免日期中输入非数字 ----
} catch(Exception NumberFormatException) {return false;
}
DateFormat df=getNewDateFormat(shortFormat);try{
df.parse(strDate);
}catch(ParseException e) {return false;
}return true;
}public static booleanisValidShortDateFormat(String strDate, String delimiter) {
String temp= strDate.replaceAll(delimiter, "");returnisValidShortDateFormat(temp);
}/*** 判断表示时间的字符是否为符合yyyyMMddHHmmss格式
*
*@paramstrDate
*@return
*/
public static booleanisValidLongDateFormat(String strDate) {if (strDate.length() !=longFormat.length()) {return false;
}try{
Long.parseLong(strDate);//---- 避免日期中输入非数字 ----
} catch(Exception NumberFormatException) {return false;
}
DateFormat df=getNewDateFormat(longFormat);try{
df.parse(strDate);
}catch(ParseException e) {return false;
}return true;
}/*** 判断表示时间的字符是否为符合yyyyMMddHHmmss格式
*
*@paramstrDate
*@paramdelimiter
*@return
*/
public static booleanisValidLongDateFormat(String strDate, String delimiter) {
String temp= strDate.replaceAll(delimiter, "");returnisValidLongDateFormat(temp);
}public staticString getShortDateString(String strDate) {return getShortDateString(strDate, "-|/");
}public staticString getShortDateString(String strDate, String delimiter) {if(StringUtil.isBlank(strDate)) {return null;
}
String temp= strDate.replaceAll(delimiter, "");if(isValidShortDateFormat(temp)) {returntemp;
}return null;
}public staticString getShortFirstDayOfMonth() {
Calendar cal=Calendar.getInstance();
Date dt= newDate();
cal.setTime(dt);
cal.set(Calendar.DAY_OF_MONTH,1);
DateFormat df=getNewDateFormat(shortFormat);returndf.format(cal.getTime());
}public staticString getWebTodayString() {
DateFormat df=getNewDateFormat(webFormat);return df.format(newDate());
}/*** 获取当月首日
*
*@return
*/
public staticString getWebFirstDayOfMonth() {
Calendar cal=Calendar.getInstance();
Date dt= newDate();
cal.setTime(dt);
cal.set(Calendar.DAY_OF_MONTH,1);
DateFormat df=getNewDateFormat(webFormat);returndf.format(cal.getTime());
}/*** 获取当月的总天数
*
*@return
*/
public static intgetDaysOfMonth() {
Calendar cal=Calendar.getInstance(Locale.CHINA);int days =cal.getActualMaximum(Calendar.DATE);returndays;
}public staticString convert(String dateString, DateFormat formatIn, DateFormat formatOut) {try{
Date date=formatIn.parse(dateString);returnformatOut.format(date);
}catch(ParseException e) {return "";
}
}public staticString convert2WebFormat(String dateString) {
DateFormat df1=getNewDateFormat(shortFormat);
DateFormat df2=getNewDateFormat(webFormat);returnconvert(dateString, df1, df2);
}public staticString convert2ChineseDtFormat(String dateString) {
DateFormat df1=getNewDateFormat(shortFormat);
DateFormat df2=getNewDateFormat(chineseDtFormat);returnconvert(dateString, df1, df2);
}public staticString convertFromWebFormat(String dateString) {
DateFormat df1=getNewDateFormat(shortFormat);
DateFormat df2=getNewDateFormat(webFormat);returnconvert(dateString, df2, df1);
}public static booleanwebDateNotLessThan(String date1, String date2) {
DateFormat df=getNewDateFormat(webFormat);returndateNotLessThan(date1, date2, df);
}/***@paramdate1
*@paramdate2
*@paramdateWebFormat2
*
*@return
*/
public static booleandateNotLessThan(String date1, String date2, DateFormat format) {try{
Date d1=format.parse(date1);
Date d2=format.parse(date2);if(d1.before(d2)) {return false;
}else{return true;
}
}catch(ParseException e) {return false;
}
}public staticString getEmailDate(Date today) {
String todayStr;
SimpleDateFormat sdf= new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
todayStr=sdf.format(today);returntodayStr;
}public staticString getSmsDate(Date today) {
String todayStr;
SimpleDateFormat sdf= new SimpleDateFormat("MM月dd日HH:mm");
todayStr=sdf.format(today);returntodayStr;
}public staticString formatMonth(Date date) {if (date == null) {return null;
}return newSimpleDateFormat(monthFormat).format(date);
}/*** 获取系统日期的前一天日期,返回Date
*
*@return
*/
public staticDate getBeforeDate() {
Date date= newDate();return new Date(date.getTime() -(ONE_DAY_MILL_SECONDS));
}/*** 获得指定时间当天起点时间
*
*@paramdate
*@return
*/
public staticDate getDayBegin(Date date) {
DateFormat df= new SimpleDateFormat("yyyyMMdd");
df.setLenient(false);
String dateString=df.format(date);try{returndf.parse(dateString);
}catch(ParseException e) {returndate;
}
}/*** 根据Date对象返回今天是星期几
*
*@paramdate
*@return1:星期日 2:星期一 3:星期二 4:星期三 5:星期四 6:星期五 7:星期六*/
public static intgetWeekDayFromDateEntity(Date date) {
Calendar calendar= Calendar.getInstance();//获得一个日历
calendar.setTime(date);int number = calendar.get(Calendar.DAY_OF_WEEK);//星期表示1-7,是从星期日开始,
returnnumber;
}/*** 判断参date上min分钟后,是否小于当前时间
*
*@paramdate
*@parammin
*@return
*/
public static boolean dateLessThanNowAddMin(Date date, longmin) {return addMinutes(date, min).before(newDate());
}public static booleanisBeforeNow(Date date) {if (date == null)return false;return date.compareTo(new Date()) < 0;
}private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//获得当前月--开始日期
public staticDate getMinMonthDate(String date) {
Calendar calendar=Calendar.getInstance();try{
calendar.setTime(dateFormat.parse(date));
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));returncalendar.getTime();
}catch(ParseException e) {return null;
}
}//获得当前月--结束日期
public staticDate getMaxMonthDate(String date) {
Calendar calendar=Calendar.getInstance();try{
calendar.setTime(dateFormat.parse(date));
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));returncalendar.getTime();
}catch(ParseException e) {return null;
}
}public static Date parseNoSecondFormat(String sDate) throwsParseException {
DateFormat dateFormat= newSimpleDateFormat(noSecondFormat);if ((sDate == null) || (sDate.length()
}if (!StringUtil.isNumeric(sDate)) {throw new ParseException("not all digit", 0);
}returndateFormat.parse(sDate);
}/**
* date日期转变成 制定格式字符串
**/
public staticString convertDate2String(Date date, String time_pattern) {
SimpleDateFormat sf= newSimpleDateFormat(time_pattern);returnsf.format(date);
}/*** 根据Date对象返回天
*
*
*@paramdate*/
public static intgetDayFromDateEntity(Date date) {
Calendar calendar= Calendar.getInstance();//获得一个日历
calendar.setTime(date);int number = calendar.get(Calendar.DATE);//星期表示1-7,是从星期日开始,
returnnumber;
}public static intcompare_date(String DATE1, String DATE2) {
DateFormat df= new SimpleDateFormat("yyyy-MM");try{
Date dt1=df.parse(DATE1);
Date dt2=df.parse(DATE2);if (dt1.getTime() >dt2.getTime()) {//System.out.println("dt1 在dt2前");
return 1;
}else if (dt1.getTime()
return -1;
}else{return 0;
}
}catch(Exception exception) {
exception.printStackTrace();
}return 0;
}public staticString getCurMonth() {return format(newDate(), webMonthFormat);
}public staticString getChineseYMString(String date) {
SimpleDateFormat sdf= newSimpleDateFormat(chineseYMFormat);try{
Date datea=sdf.parse(date);
DateFormat dateFormat=getNewDateFormat(chineseYMFormat);returngetDateString(datea, dateFormat);
}catch(ParseException ex) {
Logger.getLogger(DateUtil.class.getName()).log(Level.SEVERE, null, ex);returndate;
}
}public staticDate getPreMonthDate(String date) {
SimpleDateFormat sdf= newSimpleDateFormat(webMonthFormat);try{
Date datea=sdf.parse(date);
Calendar cal=Calendar.getInstance();
cal.setTime(datea);
cal.add(Calendar.MONTH,-1);returncal.getTime();
}catch(ParseException ex) {
Logger.getLogger(DateUtil.class.getName()).log(Level.SEVERE, null, ex);return newDate();
}
}public staticDate getNextMonthDate(String date) {
SimpleDateFormat sdf= newSimpleDateFormat(webMonthFormat);try{
Date datea=sdf.parse(date);
Calendar cal=Calendar.getInstance();
cal.setTime(datea);
cal.add(Calendar.MONTH,1);returncal.getTime();
}catch(ParseException ex) {
Logger.getLogger(DateUtil.class.getName()).log(Level.SEVERE, null, ex);return newDate();
}
}/*** 获取指定日期的当月的第一天
*
*@paramdate
*@return
*/
public staticString getAssignedDateFirstDayOfMonth(Date date) {
Calendar cal=Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
DateFormat df=getNewDateFormat(webFormat);returndf.format(cal.getTime());
}/*** 获取指定日期的当月的最后一天
*
*@paramdate
*@return
*/
public staticString getAssignedDateLastDayOfMonth(Date date) {
Calendar cal=Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
DateFormat df=getNewDateFormat(webFormat);returndf.format(cal.getTime());
}public staticDate getNextDate(Date date) {try{
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH,1);
Date date1= newDate(calendar.getTimeInMillis());returndate1;
}catch(Exception ex) {
Logger.getLogger(DateUtil.class.getName()).log(Level.SEVERE, null, ex);returndate;
}
}/*** 根据年 月 获取对应的月份 天数
**/
public static int getDaysByYearMonth(int year, intmonth) {
Calendar a=Calendar.getInstance();
a.set(Calendar.YEAR, year);
a.set(Calendar.MONTH, month- 1);
a.set(Calendar.DATE,1);
a.roll(Calendar.DATE,-1);int maxDate =a.get(Calendar.DATE);returnmaxDate;
}
}