Java实现的日期处理类完整实例
本文实例讲述了Java实现的日期处理类。分享给大家供大家参考,具体如下:
开发中常常要使用日期,先小结如下,以备后用。
importjava.text.ParseException; importjava.text.SimpleDateFormat; importjava.util.Calendar; importjava.util.Date; publicclassFormatTime{ privatefinalstaticSimpleDateFormatsdf=newSimpleDateFormat("yyyyMMdd"); privatefinalstaticSimpleDateFormatsdfymdhm=newSimpleDateFormat("yyyyMMddHHmmss"); privatefinalstaticSimpleDateFormatsdfymdhms=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss"); /** * *@Title:getCurrentDay *@Description:TODO获取当天时间(20161109) *@return */ publicstaticStringgetCurrentDay(){ returnsdf.format(newDate()); } /** * *@Title:fTime2 *@Description:TODO获取time这个日期以前dayAgo天的日期 *@return */ publicstaticStringfTime(Stringtime,intdayAgo){ Datedate=null; try{ date=sdf.parse(time); }catch(ParseExceptione){ thrownewRuntimeException(e); } Calendarcalendar=Calendar.getInstance(); calendar.setTime(date); if(dayAgo>0){ calendar.add(Calendar.DAY_OF_MONTH,-dayAgo);//前15天数据 date=calendar.getTime(); calendar.setTime(date); } intyear=calendar.get(Calendar.YEAR); intmonth=calendar.get(Calendar.MONTH)+1; intday=calendar.get(Calendar.DAY_OF_MONTH); Stringmon=""; Stringd=""; if(month<10){ mon="0"+month; }else{ mon=month+""; } if(day<10){ d="0"+day; }else{ d=""+day; } Stringret=year+""+mon+""+d; returnret; } /** * *@Title:fTime2 *@Description:TODO获取time这个日期以后dayAfter天的日期 *@return */ publicstaticStringfTime2(Stringtime,intdayAfter){ Datedate=null; try{ date=sdf.parse(time); }catch(ParseExceptione){ thrownewRuntimeException(e); } Calendarcalendar=Calendar.getInstance(); calendar.setTime(date); if(dayAfter>0){ calendar.add(Calendar.DAY_OF_MONTH,+dayAfter);//后15天数据 date=calendar.getTime(); calendar.setTime(date); } intyear=calendar.get(Calendar.YEAR); intmonth=calendar.get(Calendar.MONTH)+1; intday=calendar.get(Calendar.DAY_OF_MONTH); Stringmon=""; Stringd=""; if(month<10){ mon="0"+month; }else{ mon=month+""; } if(day<10){ d="0"+day; }else{ d=""+day; } Stringret=year+""+mon+""+d; returnret; } /** * *@Title:getDefaultTime *@Description:TODO获取昨天的日期 *@return */ publicstaticStringgetDefaultTime(){ Calendarcalendar=Calendar.getInstance(); calendar.add(Calendar.DAY_OF_MONTH,-1);//前1天 Datedate=calendar.getTime(); Stringtime=sdf.format(date); returntime; } /** * *@Title:getSunday *@Description:TODO获取最近一个星期天 *@return */ publicstaticStringgetSunday(){ SimpleDateFormatf=newSimpleDateFormat("yyyyMMdd"); Calendarc=Calendar.getInstance(); c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY); returnf.format(c.getTime()); } /** * *@Title:getMonthFirstDay *@Description:TODO获取本月第一天 *@return */ publicstaticStringgetCurrentMonthFirstDay(){ Calendarcal_1=Calendar.getInstance();//获取当前日期 cal_1.add(Calendar.MONTH,0); cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 StringfirstDay=sdf.format(cal_1.getTime()); returnfirstDay; } /** * *@Title:getMonthFirstDay *@Description:TODO获取上月第一天 *@return */ publicstaticStringgetPreviousMonthFirstDay(){ //获取当前月第一天: Calendarc=Calendar.getInstance(); c.add(Calendar.MONTH,-1); c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 Stringfirst=sdf.format(c.getTime()); returnfirst; } /** * *@Title:getMonthFirstDay *@Description:TODO获取上月最后一天 *@return */ publicstaticStringgetPreviousMonthLastDay(){ //获取当前月最后一天 Calendarca=Calendar.getInstance(); ca.set(Calendar.DAY_OF_MONTH,0);// StringlastDay=sdf.format(ca.getTime()); returnlastDay; } /** * *@Title:getCurrentMonthLastDay *@Description:TODO获取指定时间最后一天 *@return */ publicstaticStringgetCurrentMonthLastDay(Stringtime){ Datedate=null; try{ date=sdf.parse(time); }catch(ParseExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } //获取当前月最后一天 Calendarca=Calendar.getInstance(); ca.setTime(date); ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));// StringlastDay=sdf.format(ca.getTime()); returnlastDay; } /*** * *@Title:getCurrentWeekDay *@Description:TODO获取本周周一 */ publicstaticStringgetCurrentMonday(){ Calendarcal=Calendar.getInstance(); cal.setFirstDayOfWeek(Calendar.MONDAY);//将每周第一天设为星期一,默认是星期天 cal.add(Calendar.DATE,0); cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY); Stringmonday=sdf.format(cal.getTime()); returnmonday; } /*** * *@Title:getPreviousSunday *@Description:TODO获取上周周日 */ publicstaticStringgetPreviousSunday(){ Calendarcal=Calendar.getInstance(); cal.setFirstDayOfWeek(Calendar.MONDAY);//将每周第一天设为星期一,默认是星期天 cal.add(Calendar.DATE,-1*7); cal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY); Stringsunday=sdf.format(cal.getTime()); returnsunday; } /** * *@Title:getMiniSencond *@Description:TODO将日期转换为毫秒数 *@paramstr *@return */ publicstaticStringgetMiniSencond(Stringstr){ longmillionSeconds=0; try{ millionSeconds=sdfymdhm.parse(str).getTime();//毫秒 }catch(ParseExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } returnmillionSeconds+""; } /** * *@Title:getDateSencond *@Description:TODO将日期转换为毫秒数 *@paramstr *@return */ publicstaticlonggetDateSencond(Stringstr){ longmillionSeconds=0; try{ millionSeconds=sdfymdhms.parse(str).getTime();//毫秒 }catch(ParseExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } returnmillionSeconds; } /** *计算日期相差天数 *@paramstr1 *@paramstr2 *@return */ publicstaticintgetDistanceOfTwoDate(Stringstr1,Stringstr2){ intresult=0; try{ Datedate1=sdf.parse(str1); Datedate2=sdf.parse(str2); CalendaraCalendar=Calendar.getInstance(); aCalendar.setTime(date1); intday1=aCalendar.get(Calendar.DAY_OF_YEAR); aCalendar.setTime(date2); intday2=aCalendar.get(Calendar.DAY_OF_YEAR); result=day1-day2; }catch(Exceptione){ e.printStackTrace(); } returnresult; } /** * *@Title:long2Date *@Description:TODOlong转日期(年-月-日时-分-秒) *@paramtimestamp *@return */ publicstaticStringlongToDate(Longmsecond){ Datedate=newDate(msecond); returnsdfymdhms.format(date); } }
PS:这里再为大家推荐几款关于日期与天数计算的在线工具供大家使用:
在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在线万年历日历:
http://tools.jb51.net/bianmin/wannianli
在线阴历/阳历转换工具:
http://tools.jb51.net/bianmin/yinli2yangli
更多关于java相关内容感兴趣的读者可查看本站专题:《java日期与时间操作技巧汇总》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。