JAVA-各種日期算法大全_第1頁
JAVA-各種日期算法大全_第2頁
JAVA-各種日期算法大全_第3頁
JAVA-各種日期算法大全_第4頁
JAVA-各種日期算法大全_第5頁
已閱讀5頁,還剩28頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、public class DateTime /- /* * 得到系統(tǒng)當前年的數(shù)據(jù)字符串 * return strYear 返回的結(jié)果,格式 yyyy ,String 類型 * exception 得到系統(tǒng)當前年的數(shù)據(jù)字符串錯誤 */ public String getYear() String strYear=; Date currentDate = new Date(); SimpleDateFormat formatter = new SimpleDateFormat (yyyy); strYear= formatter.format(currentDate); return strYea

2、r; /- /* * 得到系統(tǒng)當前年月數(shù)據(jù)字符串 * return strYearMonth 返回的結(jié)果,格式 yyyy-MM,String 類型 * exception 得到系統(tǒng)當前年月數(shù)據(jù)字符串錯誤 */ public String getYearMonth() String strYearMonth=; Date currentDate = new Date(); SimpleDateFormat formatter = new SimpleDateFormat (yyyy-MM); strYearMonth= formatter.format(currentDate); return

3、strYearMonth; /- /* * 得到系統(tǒng)當前年月日數(shù)據(jù)字符串 * return strCurrentDate 返回的結(jié)果,格式 yyyy-MM-dd,String 類型 * exception 得到系統(tǒng)當前年月日數(shù)據(jù)字符串錯誤 */ public static String getDate() String strCurrentDate=; Date currentDate = new Date(); SimpleDateFormat formatter = new SimpleDateFormat (yyyy-MM-dd); strCurrentDate= formatter.f

4、ormat(currentDate); return strCurrentDate; /- /* * 得到系統(tǒng)當前小時,秒,分數(shù)據(jù)字符串 * return strCurrentTime 返回的結(jié)果,格式 HH:mm:ss,String 類型 * exception 得到系統(tǒng)當前小時,秒,分數(shù)據(jù)字符串錯誤 */ public String getTime() String strCurrentTime=; Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat (HH:mm:ss); str

5、CurrentTime= formatter.format(currentTime); return strCurrentTime; /- /* * 得到系統(tǒng)當前年月日小時分秒數(shù)據(jù)字符串 * return strCurrentDateTime 返回的結(jié)果,格式 yyyy-MM-dd HH:mm:ss ,String 類型 * exception 得到系統(tǒng)當前年月日小時分秒數(shù)據(jù)字符串錯誤 */ public String getDateTime() String strCurrentDateTime=; Date currentDateTime = new Date(); SimpleDateF

6、ormat formatter = new SimpleDateFormat (yyyy-MM-dd HH:mm:ss); strCurrentDateTime= formatter.format(currentDateTime); return strCurrentDateTime; /- /* * 得到系統(tǒng)當前年月日數(shù)據(jù)字符串 * return strYMD 返回的結(jié)果,格式 yyyyMMdd ,String 類型 * exception 得到系統(tǒng)當前年月日數(shù)據(jù)字符串錯誤 */ public String getYMD() String strYMD=; Date currentDateT

7、ime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat (yyyyMMdd); strYMD= formatter.format(currentDateTime); return strYMD; /- public static String getYYYYMMddHHmmss(Date date) String strYMD=; SimpleDateFormat formatter = new SimpleDateFormat (yyyyMMddHHmmss); strYMD= formatter.format(

8、date); return strYMD; /- /* * 得到對比日期變化的目標日期 getDateChange(20031015,1)=20031016; * param strCurrentDate 當前日期 格式 20000101 * param iQuantity 變化的數(shù)量 以天為單位 * return strTarget 返回的結(jié)果,格式 20000101,String 類型 * exception 得到對比日期變化的目標日期錯誤 */ public String getYMDDateChange(String strCurrentDate,int iQuantity) Stri

9、ng strTarget=; int iYear = Integer.parseInt(strCurrentDate.substring(0, 4); int iMonth = Integer.parseInt(strCurrentDate.substring(4, 6); int iDay = Integer.parseInt(strCurrentDate.substring(6, 8); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, iYear); cal.set(Calendar.MONTH, iMonth-1

10、); cal.set(Calendar.DAY_OF_MONTH, iDay); cal.add(Calendar.DATE, iQuantity); Date currentDate = cal.getTime(); SimpleDateFormat formatter = new SimpleDateFormat (yyyyMMdd); strTarget= formatter.format(currentDate); return strTarget; /- /* * 得到系統(tǒng)當前年月日小時分秒數(shù)據(jù)字符串 * return strYMDHMS 返回的結(jié)果,格式 yyyyMMddHHmms

11、s,String 類型 * exception 得到系統(tǒng)當前年月日小時分秒數(shù)據(jù)字符串錯誤 */ public static String getYMDHMS() String strYMDHMS=; Date currentDateTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat (yyyyMMddHHmmss); strYMDHMS= formatter.format(currentDateTime); return strYMDHMS; /- /* * 得到系統(tǒng)當前年月日小時分秒毫秒數(shù)據(jù)字符串 * re

12、turn strYMDHMSS 返回的結(jié)果,格式 yyyyMMddHHmmssSSS,String 類型 * exception 得到系統(tǒng)當前年月日小時分秒毫秒數(shù)據(jù)字符串錯誤 */ public static String getYMDHMSS() String strYMDHMSS=; Date currentDateTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat (yyyyMMddHHmmssSSS); strYMDHMSS= formatter.format(currentDateTime); re

13、turn strYMDHMSS; /- /* * 日期格式轉(zhuǎn)換從yyyy-MM-dd HH:mm:ss到 yyyyMMddHHmmss * param strDateTime 日期格式為yyyy-MM-dd HH:mm:ss的數(shù)據(jù) * return strYMDHMS 返回的結(jié)果, 日期格式為yyyyMMddHHmmss的數(shù)據(jù),String 類型 * exception 日期格式轉(zhuǎn)換從yyyy-MM-dd HH:mm:ss到 yyyyMMddHHmmss錯誤 */ public String stringToNumber(String strDateTime) String strYMDHMS

14、=; if(strDateTime.length()=10) strYMDHMS=strDateTime.substring(0,4)+strDateTime.substring(5,7)+strDateTime.substring(8,10); if(strDateTime.length()=19) strYMDHMS=strDateTime.substring(0,4)+strDateTime.substring(5,7)+strDateTime.substring(8,10) +strDateTime.substring(11,13)+strDateTime.substring(14,1

15、6)+strDateTime.substring(17,19); return strYMDHMS; /- /* * 日期格式轉(zhuǎn)換從yyyyMMddHHmmss到y(tǒng)yyy-MM-dd HH:mm:ss * param strYMDHMS 日期格式為yyyyMMddHHmmss的數(shù)據(jù) * return strDateTime 返回的結(jié)果, 日期格式為yyyy-MM-dd HH:mm:ss的數(shù)據(jù),String 類型 * exception 日期格式轉(zhuǎn)換從yyyyMMddHHmmss到y(tǒng)yyy-MM-dd HH:mm:ss錯誤 */ public String numberToString(Stri

16、ng strYMDHMS) String strDateTime=; if(strYMDHMS.length()=8) strDateTime=strYMDHMS.substring(0,4)+-+strYMDHMS.substring(4,6)+-+strYMDHMS.substring(6,8); if(strYMDHMS.length()=14) strDateTime=strYMDHMS.substring(0,4)+-+strYMDHMS.substring(4,6)+-+strYMDHMS.substring(6,8) + +strYMDHMS.substring(8,10)+:+

17、strYMDHMS.substring(10,12)+:+strYMDHMS.substring(12,14); return strDateTime; /- /* * 得到系統(tǒng)昨天年月日,格式 yyyyMMdd * return strYYMD 返回的結(jié)果, 日期格式為yyyyMMdd的數(shù)據(jù),String 類型 * exception 得到系統(tǒng)昨天年月日,格式 yyyyMMdd錯誤 */ public String getAgoYMD() String strYYMD=; String strYesterdayDateTime=getDateChange(getDate(),-1); Str

18、ing strYear = strYesterdayDateTime.substring(0, 4); String strMonth = strYesterdayDateTime.substring(5, 7); String strDay = strYesterdayDateTime.substring(8, 10); strYYMD= strYear+strMonth+strDay; return strYYMD; /- /* * 得到系統(tǒng)昨天年月日,格式 yyyy-MM-dd * return strYDate 返回的結(jié)果, 日期格式為yyyy-MM-dd的數(shù)據(jù),String 類型 *

19、 exception 得到系統(tǒng)昨天年月日,格式 yyyy-MM-dd錯誤 */ public String getAgoDate() String strYDate=; strYDate=getDateChange(getDate(),-1); return strYDate; /- /* * 得到系統(tǒng)上個月的年月數(shù)據(jù),格式 yyyyMM * return strYYM 返回的結(jié)果, 日期格式為yyyyMM的數(shù)據(jù),String 類型 * exception 得到系統(tǒng)上個月的年月數(shù)據(jù),格式 yyyyMM錯誤 */ public String getAgoYM() String strYYM=;

20、String strYearMonth = getYearMonth(); strYYM = getMonthChange(strYearMonth,-1); strYYM = strYYM.substring(0, 4)+strYYM.substring(5, 7); return strYYM; /- /* * 得到系統(tǒng)上個月的年月數(shù)據(jù),格式 yyyy-MM * return strYYM 返回的結(jié)果, 日期格式為yyyy-MM的數(shù)據(jù),String 類型 * exception 得到系統(tǒng)上個月的年月數(shù)據(jù)錯誤 */ public String getAgoYearMonth() String

21、 strYYearMonth=; String strYearMonth = getYearMonth(); strYYearMonth = getMonthChange(strYearMonth,-1); return strYYearMonth; /- /* * 得到系統(tǒng)當前日期是星期幾,格式 星期一 * return strCurrentWeek 返回的結(jié)果, 日期格式為星期一的數(shù)據(jù),String 類型 * exception 得到系統(tǒng)當前日期是星期幾錯誤 */ public String getWeek() String strCurrentWeek=; Date currentWee

22、k = new Date(); SimpleDateFormat formatter = new SimpleDateFormat (E); strCurrentWeek= formatter.format(currentWeek); return strCurrentWeek; /- /* * 得到任意輸入的一個日期的星期數(shù),格式 星期一 * return strWeek 返回的結(jié)果, 日期格式為星期一的數(shù)據(jù),String 類型 * exception 得到任意輸入的一個日期的星期數(shù)錯誤 */ public String getDateToWeek(String strDate) Strin

23、g strWeek=; int iYear = Integer.parseInt(strDate.substring(0, 4); int iMonth = Integer.parseInt(strDate.substring(5, 7); int iDay = Integer.parseInt(strDate.substring(8, 10); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, iYear); cal.set(Calendar.MONTH, iMonth-1); cal.set(Calendar.DAY

24、_OF_MONTH, iDay); Date currentDate = cal.getTime(); SimpleDateFormat formatter = new SimpleDateFormat(E); strWeek= formatter.format(currentDate); return strWeek; /- /* * 得到系統(tǒng)當前日期是一年中的第幾個星期 * return strCurrentWeekInYear 返回的結(jié)果,String 類型 * exception 得到系統(tǒng)當前日期是一年中的第幾個星期錯誤 */ public String getWeekInYear()

25、 String strCurrentWeekInYear=; Date currentDate = new Date(); SimpleDateFormat formatter = new SimpleDateFormat (w); strCurrentWeekInYear= formatter.format(currentDate); return strCurrentWeekInYear; /- /* * 得到當前星期日的日期 * return begin 返回的結(jié)果,Calendar 類型 * exception 得到當前星期日的日期錯誤 */ public Calendar getWe

26、ekStar() Calendar begin=Calendar.getInstance(); int iCurrentWeek = begin.get(Calendar.DAY_OF_WEEK); switch (iCurrentWeek) case 1: return begin; case 2: begin.add(Calendar.DATE, -1); return begin; case 3: begin.add(Calendar.DATE, -2); return begin; case 4: begin.add(Calendar.DATE, -3); return begin;

27、case 5: begin.add(Calendar.DATE, -4); return begin; case 6: begin.add(Calendar.DATE, -5); return begin; case 7: begin.add(Calendar.DATE, -6); return begin; default: return begin; /- /* * 得到當前星期六的日期 * return end 返回的結(jié)果,Calendar 類型 * exception 得到當前星期六的日期錯誤 */ public Calendar getWeekEnd() Calendar end=C

28、alendar.getInstance(); int iCurrentWeek = end.get(Calendar.DAY_OF_WEEK); switch (iCurrentWeek) case 1: end.add(Calendar.DATE, 6); return end; case 2: end.add(Calendar.DATE, 5); return end; case 3: end.add(Calendar.DATE, 4); return end; case 4: end.add(Calendar.DATE, 3); return end; case 5: end.add(C

29、alendar.DATE, 2); return end; case 6: end.add(Calendar.DATE, 1); return end; case 7: return end; default: return end; /- /* * 得到系統(tǒng)當前星期周日到今天的日期數(shù)據(jù) * return datelist 返回的結(jié)果,LinkedList 類型 * exception 得到系統(tǒng)當前星期周日到今天的日期數(shù)據(jù)錯誤 */ public LinkedList getSundayToToday() String strBeginDate=; String strEndDate=; Ca

30、lendar begin=getWeekStar(); Date beginDate = begin.getTime(); SimpleDateFormat formatter = new SimpleDateFormat (yyyy-MM-dd); strBeginDate= formatter.format(beginDate); Date currentDate = new Date(); strEndDate= formatter.format(currentDate); LinkedList datelist= dateToDate(strBeginDate,strEndDate);

31、 return datelist; /- /* * 得到當前星期的日期區(qū)間,格式 2003-11-23 to 2003-11-29 * return strTargetData 返回的結(jié)果, 格式 2003-11-23 to 2003-11-29,String 類型 * exception 得到當前星期的日期區(qū)間錯誤 */ public String getWeekArea() String strTargetData=; String strBeginDate=; String strEndDate=; Calendar begin=getWeekStar(); Calendar end=g

32、etWeekEnd(); Date beginDate = begin.getTime(); SimpleDateFormat formatter = new SimpleDateFormat (yyyy-MM-dd); strBeginDate= formatter.format(beginDate); Date endDate = end.getTime(); strEndDate= formatter.format(endDate); strTargetData=strBeginDate+ to +strEndDate; return strTargetData; /- /* * 得到一

33、年中的某個星期的日期區(qū)間,格式 2003-11-23 to 2003-11-29 * return strTargetData 返回的結(jié)果, 格式 2003-11-23 to 2003-11-29,String 類型 * exception 得到一年中的某個星期的日期區(qū)間錯誤 */ public String getWeekArea(int iYear,String strWeekInYear) String strTargetData=; HashMap hashm = getAllWeekArea(iYear); strTargetData=(String)hashm.get(strWee

34、kInYear); return strTargetData; /- /* * 得到一年中從開始到當前以星期為間隔的日期區(qū)間,格式 1 2000-01-01 to 2003-01-04 * return hashm 返回的結(jié)果, 格式 1 2000-01-01 to 2003-01-04,HashMap 類型 * exception 得到一年中從開始到當前以星期為間隔的日期區(qū)間錯誤 */ public HashMap getAllWeekArea(int iYear) HashMap hashm = new HashMap(); String strTargetData=; String st

35、rBeginDate=; String strEndDate=; String strWeekNum=1; Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, iYear); cal.set(Calendar.MONTH, 0); cal.set(Calendar.DAY_OF_MONTH, 1); Date currentDate = cal.getTime(); SimpleDateFormat formatter = new SimpleDateFormat (yyyy-MM-dd); strBeginDate= f

36、ormatter.format(currentDate); int iOne = cal.get(Calendar.DAY_OF_WEEK); switch (iOne) case 1: cal.add(Calendar.DATE, 6); break; case 2: cal.add(Calendar.DATE, 5); break; case 3: cal.add(Calendar.DATE, 4); break; case 4: cal.add(Calendar.DATE, 3); break; case 5: cal.add(Calendar.DATE, 2); break; case

37、 6: cal.add(Calendar.DATE, 1); break; currentDate = cal.getTime(); strEndDate= formatter.format(currentDate); strTargetData=strBeginDate+ to +strEndDate; hashm.put(strWeekNum, strTargetData); for(int i=2;i=52;i+) cal.add(Calendar.DATE, 1); currentDate = cal.getTime(); strBeginDate= formatter.format(currentDate); cal.add(Calendar.DATE, 6); currentDate = cal.getTime(); strEndDate= formatter.format(currentDate); strTargetData=strBe

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論