

您好,欢迎访问上海贝乐莉网络科技有限公司!
重要包括了对于期间操纵的少许对象办法,仅供参照吧!
packagecom..util;importjava.sql.Timestamp;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;publicclassDataUtil{/***性能:将日期对于象依照某种花样停止更动,前往改换后的字符串**@paramdate日期对于象*@parampattern改造花样例:yyyy-MM-dd*/publicstaticStringDateToString(Datedate,Stringpattern){StringstrDateTime=null;SimpleDateFormatformater=newSimpleDateFormat(pattern);strDateTime=date==null?null:formater.format(date);returnstrDateTime;}/***功效:将传进的日期对于象依照yyyy-MM-dd花样改换成字符串前往**@paramdate日期对于象*@returnString*/publicstaticStringDateToString(Datedate){String_pattern="yyyy-MM-dd";returndate==null?null:DateToString(date,_pattern);}/***效力:将传进的日期对于象依照yyyy-MM-ddHH:mm:ss花样变换成字符串前往**@paramdate日期对于象*@returnString*/publicstaticStringDateTimeToString(Datedate){String_pattern="yyyy-MM-ddHH:mm:ss";returndate==null?null:DateToString(date,_pattern);}/***效用:将拔出的字符串按花样改造成对于应的日期对于象**@paramstr字符串*@parampattern花样*@returnDate*/publicstaticDateStringToDate(Stringstr,Stringpattern){DatedateTime=null;try{if(str!=null&&!str.equals("")){SimpleDateFormatformater=newSimpleDateFormat(pattern);dateTime=formater.parse(str);}}catch(Exceptionex){}returndateTime;}/***性能:将传进的字符串按yyyy-MM-dd花样调动成对于应的日期对于象**@paramstr须要转变的字符串*@returnDate前往值*/publicstaticDateStringToDate(Stringstr){String_pattern="yyyy-MM-dd";returnStringToDate(str,_pattern);}/***功效:将传进的字符串按yyyy-MM-ddHH:mm:ss花样改变成对于应的日期对于象**@paramstr须要转变的字符串*@returnDate*/publicstaticDateStringToDateTime(Stringstr){String_pattern="yyyy-MM-ddHH:mm:ss";returnStringToDate(str,_pattern);}/***功效:将传进的字符串变换成对于应的Timestamp对于象**@paramstr待改造的字符串*@returnTimestamp变更以后的对于象*@throwsException*Timestamp*/publicstaticTimestampStringToDateHMS(Stringstr)throwsException{Timestamptime=null;time=Timestamp.valueOf(str);returntime;}/***效用:凭据传进的年头日前往响应的日期对于象**@paramyear年份*@parammonth月份*@paramday天*@returnDate日期对于象*/publicstaticDateYmdToDate(intyear,intmonth,intday){Calendarcalendar=Calendar.getInstance();calendar.set(year,month,day);returncalendar.getTime();}/***成效:将日期对于象依照MM/ddHH:mm:ss的花样停止调动,前往改造后的字符串**@paramdate日期对于象*@returnString前往值*/publicstaticStringcommunityDateToString(Datedate){SimpleDateFormatformater=newSimpleDateFormat("MM/ddHH:mm:ss");StringstrDateTime=date==null?null:formater.format(date);returnstrDateTime;}publicstaticDategetMaxDateOfDay(Datedate){if(date==null){returnnull;}else{Calendarcalendar=Calendar.getInstance();calendar.setTime(date);calendar.set(11,calendar.getActualMaximum(11));calendar.set(12,calendar.getActualMaximum(12));calendar.set(13,calendar.getActualMaximum(13));calendar.set(14,calendar.getActualMaximum(14));returncalendar.getTime();}}publicstaticDategetMinDateOfDay(Datedate){if(date==null){returnnull;}else{Calendarcalendar=Calendar.getInstance();calendar.setTime(date);calendar.set(11,calendar.getActualMinimum(11));calendar.set(12,calendar.getActualMinimum(12));calendar.set(13,calendar.getActualMinimum(13));calendar.set(14,calendar.getActualMinimum(14));returncalendar.getTime();}}/***成效:前往传进日期对于象(date)以后afterDays天数的日期对于象**@paramdate日期对于象*@paramafterDays去后天数*@returnjava.util.Date前往值*/publicstaticDategetAfterDay(Datedate,intafterDays){Calendarcal=Calendar.getInstance();cal.setTime(date);cal.add(Calendar.DATE,1);returncal.getTime();}//day/***功效:前往date1取date2出入的天数**@paramdate1*@paramdate2*@returnint*/publicstaticintDateDiff(Datedate1,Datedate2){inti=(int)((date1.getTime()-date2.getTime())/3600/24/1000);returni;}//min/***功效:前往date1取date2出入的分钟数**@paramdate1*@paramdate2*@returnint*/publicstaticintMinDiff(Datedate1,Datedate2){inti=(int)((date1.getTime()-date2.getTime())/1000/60);returni;}//second/***功用:前往date1取date2出入的秒数**@paramdate1*@paramdate2*@returnint*/publicstaticintTimeDiff(Datedate1,Datedate2){inti=(int)((date1.getTime()-date2.getTime()));returni;}}推举您浏览更多相关于“ java时辰花样化改革盘算推算 ”的作品