




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、startDateendDate =Calendar callong startTime =long endTime =longjava 中常用的與時間有關(guān)的方法 (string 與 date 轉(zhuǎn)化, 出生 日期轉(zhuǎn)年齡,時間計算周次等)java view plain copy /* * 計算兩 個日期之間相差的天數(shù) * param smdate 較小的 時間 * param bdate 較大的時間 * return 相差天數(shù) * throws ParseException */ public static int DaysMinus(Date startDate,Date endDate) t
2、hrows ParseException SimpleDateFormat sdf=newSimpleDateFormat(yyyy-MM-dd) = sdf.parse(sdf.format(startDate); sdf.parse(sdf.format(endDate);= Calendar.getInstance(); cal.setTime(startDate); cal.getTimeInMillis(); cal.setTime(endDate); cal.getTimeInMillis();between_days=(endTime-startTime)/(1000*3600*
3、24);returnInteger.parseInt(String.valueOf(between_days); /* 字符串的日期格式的計算相差天數(shù) *param 開始日期: startDate *param 結(jié)束日期: endDate *return 相差天數(shù) */ public static int DaysMinus(String startDate,String endDate) throws ParseException SimpleDateFormat sdf=new SimpleDateFormat(yyyy-MM-dd); Calendar cal = Calendar.ge
4、tInstance(); cal.setTime(sdf.parse(startDate);longstartTime = cal.getTimeInMillis(); cal.setTime(sdf.parse(endDate);long endTime= cal.getTimeInMillis();long between_days =(endTime-startTime)/(1000*3600*24); return Integer.parseInt(String.valueOf(between_days); /* String 類型日期轉(zhuǎn) Date* param date* retur
5、n* throws ParseException*/public static Date string2Date(String date) throws ParseException SimpleDateFormat sdf = new SimpleDateFormat(yyyy-MM-dd); Date resultDate = sdf.parse(date); return resultDate; /* * Date 類型日期轉(zhuǎn) String * param date * return * throws ParseException */public static String date2
6、String(Date date) throwsParseException SimpleDateFormat sdf = newSimpleDateFormat(yyyy-MM-dd);StringcurrentTime =sdf.format(date);returncurrentTime; /* 日期相加param date* param days* return*/ public static Date dateAdd(Date date, int days) Calendar calendar = new GregorianCalendar(); calendar.setTime(d
7、ate);calendar.add(calendar.DATE, days); date = calendar.getTime(); return date; /* * String 類型日期相加 * param date * param days * return * throws ParseException */public static DatedateAdd(String date, int days) throws ParseException Date Datedate = string2Date(date); Calendar calendar = new GregorianC
8、alendar(); calendar.setTime(Datedate);calendar.add(calendar.DATE, days); Datedate = calendar.getTime();return Datedate; /* * 獲取當(dāng)前日期的周次* 07 :星期日 , 01 :星期一 , 02 :星期二 , 03 :星期三 , 04 :星期四 , 05 :String DayOfWeek2MatchDict(Date dt) StringweekDays = 07, 01, 02, 03, 04, 05, 06;Calendar cal = Calendar.getIns
9、tance(); cal.setTime(dt); int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1; if getDateSysDate() throws ParseException(dayOfWeek < 0)dayOfWeek =0; return weekDaysdayOfWeek; /*獲取系統(tǒng)當(dāng)前日期 date 類型return * throwsParseException*/ public static DateDate now = new Date();SimpleDateFormat sdf = new SimpleD
10、ateFormat(yyyy-MM-dd); String sysDate = sdf.format(now); Date resultDate = sdf.parse(sysDate); return resultDate; /* * 按照傳入的格式獲取系統(tǒng) date 類型世間 -年-月-日 時:分:秒.毫秒* yyyy-MM-ddHH:mm:ss.fff: 使用 24 小時制格式化日期 *yyyy-MM-dd hh:mm:ss.fff: 使用 12 小時制格式化日期 * yyyy-MM-dd HH:mm:ss zzz* yyyy-MM-ddHH:mm:ss.ff zzz* yyyy-MM-
11、dd HH:mm:ss.fff zzz* yyyy-MM-dd HH:mm:ss.ffff zzz * param format * return* throws ParseException*/public static Date getDateSysDate(String format) throws ParseException Date now = new Date();SimpleDateFormat sdf = new SimpleDateFormat(format); String sysDate = sdf.format(now);DateresultDate = sdf.pa
12、rse(sysDate);returnresultDate; /* * 根據(jù)出生日期獲取人的 年齡 * param strBirthDate(yyyy-mm-dd oryyyy/mm/dd) * return */public staticint year =int month = int day =/計算年齡intString getPersonAgeByBirthDate(Date dateBirthDate) if (dateBirthDate = null) return ; SimpleDateFormat dateFormat = new SimpleDateFormat(yyyy
13、-MM-dd); String strBirthDate=dateFormat.format(dateBirthDate); /讀取當(dāng)前日期Calendar c =Calendar.getInstance(); c.get(Calendar.YEAR); c.get(Calendar.MONTH)+1; c.get(Calendar.DATE);age = year - Integer.parseInt(strBirthDate.substring(0, 4) - 1; if (Integer.parseInt(strBirthDate.substring(5,7) < month) a
14、ge+; else if(Integer.parseInt(strBirthDate.substring(5,7)= month && Integer.parseInt(strBirthDate.substring(8,10) <= day) age+; return String.valueOf(age); /* * 根據(jù) 出生日期獲取人的年齡 * * param strBirthDate(yyyy-mm-dd or yyyy/mm/dd)* return*/public static StringgetPersonAgeByBirthDate(StringstrBir
15、thDate)if(.equals(strBirthDate) | strBirthDate=null) return ;/讀取當(dāng)前日期 Calendar c =Calendar.getInstance();int year =c.get(Calendar.YEAR);int month =c.get(Calendar.MONTH)+1;int day =c.get(Calendar.DATE);/計算年齡intage = year - Integer.parseInt(strBirthDate.substring(0, 4) -1; if (Integer.parseInt(strBirth
16、Date.substring(5,7) < month) age+; else if(Integer.parseInt(strBirthDate.substring(5,7)= month && Integer.parseInt(strBirthDate.substring(8,10) <= day)age+; return String.valueOf(age); /* * 獲取 當(dāng)前日期是星期幾 <br>* 0-星期日 , 1- 星期一 ,2-星期二 , 3-星期三 , 4- 星期四 , 5-星期五 , 6- 星期六 * param dt* retu
17、rn */ public staticint getDayOfWeek(Date dt) Calendar cal =Calendar.getInstance(); cal.setTime(dt); int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1; return dayOfWeek; /* * 獲取當(dāng)前日期 是星期幾 * 0-星期日 , 1- 星期一 , 2- 星期二 , 3- 星 期三, 4- 星期四 , 5- 星期五 , 6- 星期六 * param:strDate * */ public static intgetDayOfWeek(S
18、tring strDate) String format=yyyy-MM-dd; /可以方便地修改日期格 式SimpleDateFormat dateFormat = newSimpleDateFormat(format);Date date = null;try date = dateFormat.parse(strDate); Calendar c = Calendar.getInstance();c.setTime(date); inte.printStackTrace(); return -1; /* * 獲取系統(tǒng)當(dāng)前時間return dayOfWeek; catch (ParseException e)*/ public static Str
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 臨床診斷學(xué)模擬試題及答案
- 鋼鐵材料采購預(yù)付款合同
- 畢業(yè)生就業(yè)跟蹤與職業(yè)發(fā)展支持協(xié)議
- 壓實安全生產(chǎn)各方責(zé)任
- 班前安全培訓(xùn)內(nèi)容
- 餐飲安全生產(chǎn)責(zé)任制度表
- 安全生產(chǎn)月活動報道稿件
- 新時代鄉(xiāng)村教師發(fā)展的內(nèi)涵與路徑探討
- 2025年西安醫(yī)學(xué)院附屬寶雞醫(yī)院第三批招聘(25人)筆試歷年專業(yè)考點(難、易錯點)附帶答案詳解
- 2025年浙江醫(yī)療衛(wèi)生招聘溫州永嘉縣人民醫(yī)院醫(yī)共體招聘勞務(wù)派遣人員1人筆試歷年專業(yè)考點(難、易錯點)附帶答案詳解
- 南京師范大學(xué)古代漢語教案
- 馬工程西方經(jīng)濟(jì)學(xué)(精要本第三版)教案
- 引水隧洞工程安全施工方案
- 2025年麻風(fēng)病防治知識競賽復(fù)習(xí)試題庫完整
- 2025浙江高考:歷史必考知識點歸納
- 食品安全員培訓(xùn)大綱
- 運營酒店公寓管理制度
- DB32T 5082-2025建筑工程消防施工質(zhì)量驗收標(biāo)準(zhǔn)
- 2025年高中語文必修下文言文《諫逐客書》知識點梳理
- 2025-2030全球及中國牙科保險服務(wù)行業(yè)市場現(xiàn)狀供需分析及投資評估規(guī)劃分析研究報告
- 數(shù)獨教案-完整版
評論
0/150
提交評論