Java日期处理工具类DateUtils详解
本文实例为大家分享了Java日期处理工具类DateUtils的具体代码,供大家参考,具体内容如下
importjava.sql.Timestamp;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
importjava.util.Date;
/**
*<日期时间处理工具类>
*/
publicclassDateUtils{
/**
*Dateformatpatternthisisoftenused.
*/
publicstaticfinalStringPATTERN_YMD="yyyy-MM-dd";
/**
*Dateformatpatternthisisoftenused.
*/
publicstaticfinalStringPATTERN_YMDHMS="yyyy-MM-ddHH:mm:ss";
/**
*FormatsthegivendateaccordingtotheYMDpattern.
*
*@paramdateThedatetoformat.
*@returnAnYMDformatteddatestring.
*
*@see#PATTERN_YMD
*/
publicstaticStringformatDate(Datedate){
returnformatDate(date,PATTERN_YMD);
}
/**
*Formatsthegivendateaccordingtothespecifiedpattern.Thepattern
*mustconformtothatusedbythe{@linkSimpleDateFormatsimpledate
*format}class.
*
*@paramdateThedatetoformat.
*@parampatternThepatterntouseforformattingthedate.
*@returnAformatteddatestring.
*
*@throwsIllegalArgumentExceptionIfthegivendatepatternisinvalid.
*
*@seeSimpleDateFormat
*/
publicstaticStringformatDate(Datedate,Stringpattern){
if(date==null)
thrownewIllegalArgumentException("dateisnull");
if(pattern==null)
thrownewIllegalArgumentException("patternisnull");
SimpleDateFormatformatter=newSimpleDateFormat(pattern);
returnformatter.format(date);
}
/**
*Parsesadatevalue.Theformatusedforparsingthedatevalueareretrievedfrom
*thedefaultPATTERN_YMD.
*
*@paramdateValuethedatevaluetoparse
*
*@returntheparseddate
*
*@throwsIllegalArgumentExceptionIfthegivendateValueisinvalid.
*/
publicstaticDateparseDate(StringdateValue){
returnparseDate(dateValue,null);
}
/**
*Parsesthedatevalueusingthegivendateformat.
*
*@paramdateValuethedatevaluetoparse
*@paramdateFormatthedateformattouse
*
*@returntheparseddate.ifparseisfailed,returnnull
*
*@throwsIllegalArgumentExceptionIfthegivendateValueisinvalid.
*/
publicstaticDateparseDate(StringdateValue,StringdateFormat){
if(dateValue==null){
thrownewIllegalArgumentException("dateValueisnull");
}
if(dateFormat==null){
dateFormat=PATTERN_YMD;
}
SimpleDateFormatdf=newSimpleDateFormat(dateFormat);
Dateresult=null;
try{
result=df.parse(dateValue);
}
catch(ParseExceptionpe){
pe.printStackTrace();//日期型字符串格式错误
}
returnresult;
}
/**
*Addsanumberofyearstoadatereturninganewobject.
*Theoriginaldateobjectisunchanged.
*
*@paramdatethedate,notnull
*@paramamounttheamounttoadd,maybenegative
*@returnthenewdateobjectwiththeamountadded
*@throwsIllegalArgumentExceptionifthedateisnull
*/
publicstaticDateaddYears(Datedate,intamount){
returnadd(date,Calendar.YEAR,amount);
}
/**
*Addsanumberofyearstoatimestampreturninganewobject.
*Theoriginaltimestampobjectisunchanged.
*
*@paramtimestampthetimestamp,notnull
*@paramamounttheamounttoadd,maybenegative
*@returnthenewtimestampobjectwiththeamountadded
*@throwsIllegalArgumentExceptionifthetimestampisnull
*/
publicstaticTimestampaddYears(Timestamptimestamp,intamount){
returnadd(timestamp,Calendar.YEAR,amount);
}
//-----------------------------------------------------------------------
/**
*Addsanumberofmonthstoadatereturninganewobject.
*Theoriginaldateobjectisunchanged.
*
*@paramdatethedate,notnull
*@paramamounttheamounttoadd,maybenegative
*@returnthenewdateobjectwiththeamountadded
*@throwsIllegalArgumentExceptionifthedateisnull
*/
publicstaticDateaddMonths(Datedate,intamount){
returnadd(date,Calendar.MONTH,amount);
}
/**
*Addsanumberofmonthstoatimestampreturninganewobject.
*Theoriginaltimestampobjectisunchanged.
*
*@paramtimestampthetimestamp,notnull
*@paramamounttheamounttoadd,maybenegative
*@returnthenewtimestampobjectwiththeamountadded
*@throwsIllegalArgumentExceptionifthetimestampisnull
*/
publicstaticTimestampaddMonths(Timestamptimestamp,intamount){
returnadd(timestamp,Calendar.MONTH,amount);
}
//-----------------------------------------------------------------------
/**
*Addsanumberofdaystoadatereturninganewobject.
*Theoriginaldateobjectisunchanged.
*
*@paramdatethedate,notnull
*@paramamounttheamounttoadd,maybenegative
*@returnthenewdateobjectwiththeamountadded
*@throwsIllegalArgumentExceptionifthedateisnull
*/
publicstaticDateaddDays(Datedate,intamount){
returnadd(date,Calendar.DATE,amount);
}
/**
*Addsanumberofdaystoatimestampreturninganewobject.
*Theoriginaltimestampobjectisunchanged.
*
*@paramtimestampthetimestamp,notnull
*@paramamounttheamounttoadd,maybenegative
*@returnthenewtimestampobjectwiththeamountadded
*@throwsIllegalArgumentExceptionifthetimestampisnull
*/
publicstaticTimestampaddDays(Timestamptimestamp,intamount){
returnadd(timestamp,Calendar.DATE,amount);
}
//-----------------------------------------------------------------------
/**
*Addsanumberofminutestoatimestampreturninganewobject.
*Theoriginaltimestampobjectisunchanged.
*
*@paramtimestampthetimestamp,notnull
*@paramamounttheamounttoadd,maybenegative
*@returnthenewtimestampobjectwiththeamountadded
*@throwsIllegalArgumentExceptionifthetimestampisnull
*/
publicstaticTimestampaddMinutes(Timestamptimestamp,intamount){
returnadd(timestamp,Calendar.MINUTE,amount);
}
/**
*Addsanumberofdaystocurrenttimereturninganewobject.
*
*@paramamounttheamounttoadd,maybenegative
*@returnthenewtimestampobjectwiththeamountadded
*/
publicstaticTimestampaddDays(intamount){
Calendarc=Calendar.getInstance();
c.add(Calendar.DATE,amount);
returnnewTimestamp(c.getTimeInMillis());
}
//-----------------------------------------------------------------------
/**
*Addstoadatereturninganewobject.
*Theoriginaldateobjectisunchanged.
*
*@paramdatethedate,notnull
*@paramcalendarFieldthecalendarfieldtoaddto
*@paramamounttheamounttoadd,maybenegative
*@returnthenewdateobjectwiththeamountadded
*@throwsIllegalArgumentExceptionifthedateisnull
*/
privatestaticDateadd(Datedate,intcalendarField,intamount){
if(date==null){
thrownewIllegalArgumentException("Thedatemustnotbenull");
}
Calendarc=Calendar.getInstance();
c.setTime(date);
c.add(calendarField,amount);
returnc.getTime();
}
/**
*Addstoatimestampreturninganewobject.
*Theoriginaltimestampobjectisunchanged.
*
*@paramtimestampthetimestamp,notnull
*@paramcalendarFieldthecalendarfieldtoaddto
*@paramamounttheamounttoadd,maybenegative
*@returnthenewtimestampobjectwiththeamountadded
*@throwsIllegalArgumentExceptionifthetimestampisnull
*/
privatestaticTimestampadd(Timestamptimestamp,intcalendarField,intamount){
if(timestamp==null){
thrownewIllegalArgumentException("Thetimestampmustnotbenull");
}
Calendarc=Calendar.getInstance();
c.setTime(timestamp);
c.add(calendarField,amount);
returnnewTimestamp(c.getTimeInMillis());
}
/**
*<生成最小的当天日期值>
*@return最小的当天日期值
*/
publicstaticTimestampnow(){
Calendarc=Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY,0);
c.set(Calendar.MINUTE,0);
c.set(Calendar.SECOND,0);
c.set(Calendar.MILLISECOND,0);
returnnewTimestamp(c.getTimeInMillis());
}
/**Thisclassshouldnotbeinstantiated.*/
privateDateUtils(){
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。