




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、java中常用的與時間有關(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(DatestartDate,Date endDate) throws ParseExceptionSimpleDateFormatsdf=newSimpleDateFormat(yyyy-MM-dd);startDate= s
2、df.parse(sdf.format(startDate);endDate =sdf.parse(sdf.format(endDate);Calendar cal between_days=(endTime-startTime)/(1000*3600*24);= Calendar.getInstance(); cal.setTime(startDate);= Calendar.getInstance(); cal.setTime(startDate);cal.getTimeInMillis();cal.setTime(endDate);cal.getTimeInMillis();long s
3、tartTime =long endTime =longreturnInteger.parseInt(String.valueOf(between_days);/* 字符串的日期格式的計算相差天數(shù) *param 開始日期:startDate *param 結(jié)束日期: endDate *return 相差天數(shù) */ public static int DaysMinus(String startDate,String endDate) throws ParseExceptionSimpleDateFormatsdf=new SimpleDateFormat(yyyy-MM-dd);Calenda
4、r cal = Calendar.getInstance();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);returnInteger.parseInt(String.valueOf(between_days); /* String 類型日期轉(zhuǎn) Date * p
5、aram datereturn * throws ParseException */public static Date string2Date(String date) throws ParseExceptionSimpleDateFormat sdf = newSimpleDateFormat(yyyy-MM-dd);DateresultDate = sdf.parse(date);returnresultDate; /* Date 類型日期轉(zhuǎn) Stringparam date * return * throws ParseException */ public static String
6、 date2String(Date date) throwsParseException SimpleDateFormat sdf = new SimpleDateFormat(yyyy-MM-dd); String currentTime = sdf.format(date); return currentTime; /* * 日期相加 * param date * param days * return */ public static Date dateAdd(Date date, int days) Calendar calendar = new GregorianCalendar()
7、; calendar.setTime(date);calendar.add(calendar.DATE, days); date = calendar.getTime(); return date; /* * String 類型日期相加 * param date * param days * return * throws ParseException */ public static Date dateAdd(String date, int days) throws ParseException Date Datedate = string2Date(date); Calendar cal
8、endar = new GregorianCalendar(); calendar.setTime(Datedate);calendar.add(calendar.DATE, days); Datedate = calendar.getTime(); return Datedate; /* * 獲取當(dāng)前日期的周次* 07:星期日, 01:星期一, 02:星期二, 03:星期三, 04:星期四, 05: 星期五, 06:星期六* author 秦海 *param dt * return */ public static String DayOfWeek2MatchDict(Date dt) St
9、ring weekDays = 07, 01, 02, 03, 04, 05, 06; Calendar cal = Calendar.getInstance();cal.setTime(dt); int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1; if (dayOfWeek 0) dayOfWeek = 0; return weekDaysdayOfWeek; /* * 獲取系統(tǒng) 當(dāng)前日期 date 類型 * return * throws ParseException */ public static Date getDateSysDate
10、() throws ParseException Date now = new Date();SimpleDateFormat sdf = new SimpleDateFormat(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
11、:使用12小時制格式化日期 * yyyy-MM-dd HH:mm:ss zzz * yyyy-MM-ddHH:mm:ss.ff zzz * yyyy-MM-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
12、 SimpleDateFormat(format); String sysDate = sdf.format(now);DateresultDate = sdf.parse(sysDate);returnresultDate; /* * 根據(jù)出生日期獲取人的 年齡 * * param strBirthDate(yyyy-mm-dd or yyyy/mm/dd) * return */ public static String getPersonAgeByBirthDate(Date dateBirthDate) if (dateBirthDate = null) return ; Simple
13、DateFormat dateFormat = new SimpleDateFormat(yyyy-MM-dd); String strBirthDate=dateFormat.format(dateBirthDate);Calendar c =Calendar c =Calendar.getInstance();int year =c.get(Calendar.YEAR);int month =c.get(Calendar.MONTH)+1;c.get(Calendar.DATE);Calendar.getInstance();int year =c.get(Calendar.YEAR);i
14、nt month =c.get(Calendar.MONTH)+1;c.get(Calendar.DATE);int day =/計算年齡intage = year - Integer.parseInt(strBirthDate.substring(0, 4) - 1; if (Integer.parseInt(strBirthDate.substring(5,7) month) age+; else if (Integer.parseInt(strBirthDate.substring(5,7)= month & Integer.parseInt(strBirthDate.substring
15、(8,10) = day) age+; return String.valueOf(age); /* * 根據(jù) 出生日期獲取人的年齡 * * param strBirthDate(yyyy-mm-dd or yyyy/mm/dd) * return */ public static String getPersonAgeByBirthDate(StringstrBirthDate)if(.equals(strBirthDate) | strBirthDate =null) return ;/讀取當(dāng)前日期 Calendar c=Calendar.getInstance();int year =c
16、.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(strBirthDate.substring(5,7)(Integer.parseInt(strBirthDate.substring(5,7)= month & Integer.parseInt(strBirthDate.substring(8
17、,10) = day) age+; return String.valueOf(age); /* * 獲取 當(dāng)前日期是星期幾 * 0-星期日,1-星期一, 2-星期二, 3-星期三, 4-星期四, 5-星期五, 6-星期六 * param dt * return*/ public staticint getDayOfWeek(Date dt) Calendar cal =Calendar.getInstance();cal.setTime(dt);int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1; return dayOfWeek; /* 獲取
18、當(dāng)前日期是星期幾 * 0-星期日, 1-星期一, 2-星期二, 3-星 期三, 4-星期四, 5-星期五, 6-星期六* param:strDate */ public static intgetDayOfWeek(String strDate)Stringformat=yyyy-MM-dd;/可以方便地修改日期格式SimpleDateFormat dateFormat = newSimpleDateFormat(format);Date date = null;try date = dateFormat.parse(strDate);Calendar c = Calendar.getInstance();c.setTime(date);int dayOfWeek=c.get(Calendar.DAY_OF_WEEK)- 1;e.printStackTrace(); return -1; /* * 獲取系統(tǒng)當(dāng)前時間return dayOfWeek; catch (ParseException e)*/ pub
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 眉山職業(yè)技術(shù)學(xué)院《花紙設(shè)計與排版》2023-2024學(xué)年第二學(xué)期期末試卷
- 質(zhì)量標(biāo)準(zhǔn)在醫(yī)療安全中的作用與實踐
- 沈陽化工大學(xué)《城市復(fù)興設(shè)計》2023-2024學(xué)年第二學(xué)期期末試卷
- 高效掌握企業(yè)財務(wù)分析與決策支持技巧
- 圖片視頻等多媒體緩存方案
- 河北工藝美術(shù)職業(yè)學(xué)院《虛擬現(xiàn)實引擎技術(shù)》2023-2024學(xué)年第二學(xué)期期末試卷
- 2025年銅鼓縣五下數(shù)學(xué)期末調(diào)研模擬試題含答案
- 承德醫(yī)學(xué)院《建筑及規(guī)劃設(shè)計5(上)》2023-2024學(xué)年第二學(xué)期期末試卷
- 廣西南寧市良慶區(qū)2025年三下數(shù)學(xué)期末統(tǒng)考模擬試題含解析
- 貴陽信息科技學(xué)院《流行趨勢預(yù)測與應(yīng)用》2023-2024學(xué)年第二學(xué)期期末試卷
- 2024-2030年中國長管拖車行業(yè)市場發(fā)展趨勢與前景展望戰(zhàn)略分析報告
- 《高等教育學(xué)》近年考試真題題庫(含答案)
- 2024福建省廈門市總工會擬錄用人員筆試歷年典型考題及考點剖析附答案帶詳解
- 2024風(fēng)力發(fā)電機(jī)組預(yù)應(yīng)力基礎(chǔ)錨栓籠組合件技術(shù)規(guī)范
- 供熱管道施工組織設(shè)計
- 浙江省中小學(xué)心理健康教育教師上崗資格證書管理辦法(修訂)
- 2024年青島港灣職業(yè)技術(shù)學(xué)院單招職業(yè)適應(yīng)性測試題庫審定版
- 2024年時事政治題(考點梳理)
- 2023全國乙卷語文真題試卷及答案解析
- JavaWeb程序設(shè)計 教案 第1章 JavaWeb開發(fā)環(huán)境配置-2學(xué)時
- 初中九年級美術(shù)期末藝術(shù)測評指標(biāo)試卷及答案
評論
0/150
提交評論