資源描述:
《java常用日期格式處理》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在應(yīng)用文檔-天天文庫(kù)。
1、/***取當(dāng)前日期*@return當(dāng)前日期的字符串,如2002-12-12**/publicstaticStringtoday(){java.text.SimpleDateFormatformatter=newjava.text.SimpleDateFormat("yyyy-MM-dd");java.util.DatecurrentTime_1=newjava.util.Date();StringdateString=formatter.format(currentTime_1);returndateString;}/***取當(dāng)前時(shí)間,*@return當(dāng)前時(shí)間,如:21
2、:10:12**/publicstaticStringtime(){java.text.SimpleDateFormatformatter=newjava.text.SimpleDateFormat("HH:mm:ss");java.util.DatecurrentTime_1=newjava.util.Date();StringdateString=formatter.format(currentTime_1);returndateString;}/***將java.util.date型按照指定格式轉(zhuǎn)為字符串*@paramd源對(duì)象*@paramsFormatps*@r
3、eturn如:2003-01-01*/privatestaticStringtoString(java.util.Dated,StringsFormat){java.text.SimpleDateFormatformatter=newjava.text.SimpleDateFormat(sFormat);StringdateString=formatter.format(d);returndateString;}/***強(qiáng)制類型轉(zhuǎn)換從串到日期*@paramsDate源字符串,采用yyyy-MM-dd格式*@paramsFormatps*@return得到的日期對(duì)象*@t
4、hrowsParseException*/publicstaticjava.util.DateparseDate(StringsDate,StringsFormat)throwsParseException{java.text.SimpleDateFormatformatter=null;java.util.Dateutildate=null;formatter=newjava.text.SimpleDateFormat(sFormat);utildate=formatter.parse(sDate);returnutildate;}/***強(qiáng)制類型轉(zhuǎn)換從串到時(shí)間戳*@
5、paramsDate源串*@paramsFormat遵循格式*@return取得的時(shí)間戳對(duì)象*@throwsParseException*/publicstaticTimestampparseTimestamp(StringsDate,StringsFormat)throwsParseException{java.text.SimpleDateFormatformatter=null;java.util.Dateutildate=null;formatter=newjava.text.SimpleDateFormat(sFormat);utildate=formatte
6、r.parse(sDate);java.sql.Timestamptsdate=newjava.sql.Timestamp(utildate.getTime());returntsdate;}/***getCurDate取當(dāng)前日期*@returnjava.util.Date型日期**/publicstaticjava.util.DategetCurDate(){return(newjava.util.Date());}/***getCurTimestamp取當(dāng)前時(shí)間戳*@returnjava.sql.Timestamp**/publicstaticjava.sql.Ti
7、mestampgetCurTimestamp(){java.util.Datetoday=newjava.util.Date();java.sql.Timestampts=newjava.sql.Timestamp(today.getTime());returnts;}