java处理日期的工具类DateUtil
java中处理日期的工具类DateUtil,供大家参考,具体内容如下
packagecom.leo.demo.othertest; importorg.slf4j.LoggerFactory; importjava.sql.Timestamp; importjava.text.DateFormat; importjava.text.ParseException; importjava.text.SimpleDateFormat; importjava.util.Calendar; importjava.util.Date; importjava.util.GregorianCalendar; importjava.util.logging.Logger; /** *时间工具类 */ publicfinalclassDateUtil{ privatestaticLoggerlogger=(Logger)LoggerFactory.getLogger(DateUtil.class); /** *日期格式 */ publicinterfaceDATE_PATTERN{ StringHHMMSS="HHmmss"; StringHH_MM_SS="HH:mm:ss"; StringHH_MM="HH:mm"; StringYYYY="yyyy"; StringYYYYMMDD="yyyyMMdd"; StringYYYYMM="yyyyMM"; StringYYYY_MM_DD="yyyy-MM-dd"; StringYYYYMMDDHHMMSS="yyyyMMddHHmmss"; StringYYYYMMDDHHMMSSSSS="yyyyMMddHHmmssSSS"; StringYYYY_MM_DD_HH_MM_SS="yyyy-MM-ddHH:mm:ss"; } /** *获取当前时间 * *@returnTimestamp对象 */ publicstaticTimestampgetCurrontTime(){ TimestampsqlTimestamp=newTimestamp(newDate().getTime()); returnsqlTimestamp; } /** *将Date类型转换成String类型 * *@paramdateDate对象 *@return形如:"yyyy-MM-ddHH:mm:ss" */ publicstaticStringdate2String(Datedate){ returndate2String(date,DATE_PATTERN.YYYY_MM_DD_HH_MM_SS); } /** *将Date按格式转化成String * *@paramdateDate对象 *@parampattern日期类型 *@returnString */ publicstaticStringdate2String(Datedate,Stringpattern){ if(date==null||pattern==null){ returnnull; } returnnewSimpleDateFormat(pattern).format(date); } /** *将String类型转换成Date类型 * *@paramdateDate对象 *@return */ publicstaticDatestring2Date(Stringdate){ SimpleDateFormatformat=newSimpleDateFormat(DATE_PATTERN.YYYY_MM_DD_HH_MM_SS); try{ returnformat.parse(date); }catch(ParseExceptione){ returnnull; } } /** *获取某日期N天后的日期 * *@paramdatestr *@paramday *@return */ publicstaticDategetBeforeAfterDate(Stringdatestr,intday){ SimpleDateFormatdf=newSimpleDateFormat(DATE_PATTERN.YYYY_MM_DD_HH_MM_SS); java.sql.Dateolddate=null; try{ df.setLenient(false); olddate=newjava.sql.Date(df.parse(datestr).getTime()); }catch(ParseExceptione){ thrownewRuntimeException("日期转换错误"); } Calendarcal=newGregorianCalendar(); cal.setTime(olddate); intYear=cal.get(Calendar.YEAR); intMonth=cal.get(Calendar.MONTH); intDay=cal.get(Calendar.DAY_OF_MONTH); intNewDay=Day+day; cal.set(Calendar.YEAR,Year); cal.set(Calendar.MONTH,Month); cal.set(Calendar.DAY_OF_MONTH,NewDay); returnnewDate(cal.getTimeInMillis()); } /** *@return *@Description:获取当前日期的前一天 *@ReturnTypeString *@author:liyl *@Created2015年11月13日下午5:11:14 */ publicstaticDatecurrentBeforeDay(){ Calendarcalendar=Calendar.getInstance(); calendar.setTime(newDate()); calendar.add(Calendar.DAY_OF_MONTH,-1); returncalendar.getTime(); } /** *@return *@Description:获取当前日期的后一天 *@ReturnTypeDate *@author:liyl *@Created2015年11月13日下午5:14:54 */ publicstaticDatecurrentNextDay(){ Calendarcalendar=Calendar.getInstance(); calendar.setTime(newDate()); calendar.add(Calendar.DAY_OF_MONTH,1); returncalendar.getTime(); } /** *获取指定日期星期几(int) * *@paramdt *@return */ publicstaticintgetWeekOfInt(Datedt){ int[]weekDays={7,1,2,3,4,5,6}; Calendarcal=Calendar.getInstance(); cal.setTime(dt); intw=cal.get(Calendar.DAY_OF_WEEK)-1; if(w<0){ w=0; } returnweekDays[w]; } /** *获取指定日期星期几 * *@paramdt *@return */ publicstaticStringgetWeekOfDate(Datedt){ String[]weekDays={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; Calendarcal=Calendar.getInstance(); cal.setTime(dt); intw=cal.get(Calendar.DAY_OF_WEEK)-1; if(w<0){ w=0; } returnweekDays[w]; } /** *时间比大小 * *@paramDATE1 *@paramDATE2 *@parampattern *@return */ publicstaticintcompareDate(StringDATE1,StringDATE2,Stringpattern){ DateFormatdf=newSimpleDateFormat(pattern); try{ Datedt1=df.parse(DATE1); Datedt2=df.parse(DATE2); if(dt1.getTime()>dt2.getTime()){ System.out.println("dt1在dt2前"); return1; }elseif(dt1.getTime()-1){ result=date.replace(".","-"); }elseif(date.indexOf("年")>-1){ result=date.replace("年","-").replace("月","-").replace("日",""); }elseif(date.indexOf("-")>-1){ result=date.replace("年","-").replace("月","-").replace("日",""); }else{ result=date; } returnresult; } /** *获取两个日期相差的月数 * *@paramd1较大的日期 *@paramd2较小的日期 *@return如果d1>d2返回月数差否则返回0 */ publicstaticintmonthsBetween(Dated1,Dated2){ Calendarc1=Calendar.getInstance(); Calendarc2=Calendar.getInstance(); c1.setTime(d1); c2.setTime(d2); if(c1.getTimeInMillis() d2返回月数差否则返回0 */ publicstaticintdaysBetween(Datedate1,Datedate2){ DateFormatsdf=newSimpleDateFormat(DATE_PATTERN.YYYYMMDD); Calendarcal=Calendar.getInstance(); try{ Dated1=sdf.parse(date2String(date1,DATE_PATTERN.YYYYMMDD)); Dated2=sdf.parse(date2String(date2,DATE_PATTERN.YYYYMMDD)); cal.setTime(d1); longtime1=cal.getTimeInMillis(); cal.setTime(d2); longtime2=cal.getTimeInMillis(); returnInteger.parseInt(String.valueOf((time2-time1)/86400000L)); }catch(Exceptione){ e.printStackTrace(); } return0; } /** *计算date2-date1之间相差的分钟 * *@paramdate1 *@paramdate2 *@return */ @SuppressWarnings("deprecation") publicstaticintminutesBetween(Datedate1,Datedate2){ Calendarcal=Calendar.getInstance(); //date1.setSeconds(0); cal.setTime(date1); longtime1=cal.getTimeInMillis(); cal.setTime(date2); longtime2=cal.getTimeInMillis(); if(time2-time1<=0){ return0; }else{ returnInteger.parseInt(String.valueOf((time2-time1)/60000L))+1; } } /** *计算date2-date1之间相差的秒 * *@paramdate1 *@paramdate2 *@return */ @SuppressWarnings("deprecation") publicstaticintsecondBetween(Datedate1,Datedate2){ Calendarcal=Calendar.getInstance(); //date1.setSeconds(0); cal.setTime(date1); longtime1=cal.getTimeInMillis(); cal.setTime(date2); longtime2=cal.getTimeInMillis(); if(time2-time1<=0){ return0; }else{ returnInteger.parseInt(String.valueOf((time2-time1)/1000L))+1; } } /** *计算date2-date1之间相差的毫秒 * *@paramdate1 *@paramdate2 *@return */ @SuppressWarnings("deprecation") publicstaticintmillisecondBetween(Datedate1,Datedate2){ Calendarcal=Calendar.getInstance(); cal.setTime(date1); longtime1=cal.getTimeInMillis(); cal.setTime(date2); longtime2=cal.getTimeInMillis(); if(time2-time1<=0){ return0; }else{ returnInteger.parseInt(String.valueOf((time2-time1))); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。