字符串和日期的轉(zhuǎn)換_百度文庫(kù)_第1頁(yè)
字符串和日期的轉(zhuǎn)換_百度文庫(kù)_第2頁(yè)
字符串和日期的轉(zhuǎn)換_百度文庫(kù)_第3頁(yè)
字符串和日期的轉(zhuǎn)換_百度文庫(kù)_第4頁(yè)
字符串和日期的轉(zhuǎn)換_百度文庫(kù)_第5頁(yè)
已閱讀5頁(yè),還剩3頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、java開(kāi)發(fā)時(shí)往往需要做字符串和日期的轉(zhuǎn)換主要用到:java.text.SimpleDateFormatSimpleDateFormat sdf = new SimpleDateFormat( yyyy-MM-dd HH:mm:ss ; 它確立了轉(zhuǎn)換的格式,為什么有的格式大寫(xiě),有的格式小寫(xiě),那是怕避免混淆,例如MM是月份,mm是分;HH是24小 時(shí)制,而hh是12小時(shí)制1.字符串轉(zhuǎn)日期 2008-07-10 19:20:00 要把它轉(zhuǎn)成日期,可以用 Date date = sdf.parse( 2008-07-10 19:20:00 ;2.日期轉(zhuǎn)字符串 假如把今天的日期轉(zhuǎn)成字符串可用 Stri

2、ng str = sdf.format(new Date(;這個(gè)字符串內(nèi)容的格式類似2008-07-10 19:20:00。附編寫(xiě)好的一個(gè)簡(jiǎn)單實(shí)例:import java.util.Date; import java.text.ParseException; import java.text.SimpleDateFormat; public class ConvertDemo /* */* * 日期轉(zhuǎn)換成字符串 * param date * return str */ public static String DateToStr(Date date SimpleDateFormat forma

3、t = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss; String str = format.format(date; return str; /* */* * 字符串轉(zhuǎn)換成日期 * param str * return date */ public static Date StrToDate(String str SimpleDateFormat format = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss; Date date = null; try date = format.parse(str; catch (

4、ParseException e e.printStackTrace(; return date; public static void main(String args Date date = new Date(; System.out.println(日期轉(zhuǎn)字符串: + ConvertDemo.DateToStr(date; System.out.println(字符串轉(zhuǎn)日期: + ConvertDemo.StrToDate(ConvertDemo.DateToStr(date; import java.text.ParseException; import java.text.Simpl

5、eDateFormat; import java.util.Date; /* */* * JAVA中字符串轉(zhuǎn)與日期型的互轉(zhuǎn)實(shí)例 */ public class ExValue /* */* * 取當(dāng)前系統(tǒng)日期,并按指定格式或者是默認(rèn)格式返回 * param format * return */ public static String getNow(String format if(null = format | .equals(format format = yyyy-MM-dd HH:mm:ss; SimpleDateFormat sdf = new SimpleDateFormat(fo

6、rmat; String date = sdf.format(new Date(; return date; /* */* * 將字符型轉(zhuǎn)換為指定格式日期型 * param _date 需要轉(zhuǎn)換成日期的字符串 * param format 與需要轉(zhuǎn)換成日期的字符串相匹配的格式 * return */ private static Date stringToDate(String _date,String format if(null = format | .equals(format format = yyyy-MM-dd HH:mm:ss; SimpleDateFormat sdf = ne

7、w SimpleDateFormat(format; Date date=null; try date=sdf.parse(_date; catch (ParseException e / TODO Auto-generated catch block e.printStackTrace(; return date; /* */* * 將日期型轉(zhuǎn)換為指定格式的字符串 * param date 日期 * param format 格式 * return */ public static String dateToString(Date date,String format if(null = f

8、ormat | .equals(format format = yyyy年MM月dd日 hh點(diǎn):mm分:ss秒; SimpleDateFormat sdf = new SimpleDateFormat(format; return sdf.format(date; public static void main(String args throws Exception /當(dāng)字符串轉(zhuǎn)日期型的時(shí)候,format的格式,一定要和需要轉(zhuǎn)成日期的字符串相匹配 /String dateString=2009-01-01,format=yyyy-MM-dd; String dateString=2009-0

9、1-01 01:01:02,format=yyyy-MM-dd hh:mm:ss; String result=stringToDate(dateString, format.toString(; System.out.println(字符轉(zhuǎn)日期:+result.toString(; System.out.println(日期轉(zhuǎn)字符串:+dateToString(stringToDate(dateString, format,yyyy年MM月dd日 hh點(diǎn):mm分:ss秒; System.currentTimeMillis(;/以毫秒為單位返回當(dāng)前時(shí)間java.util.CalendarCal

10、endar g=Calendar.getInstance(; g.add(Calendar.YEAR,1; SimpleDateFormat s=new SimpleDateFormat(yyyy-MM-dd HH-mm-ss,Locale.US; String d=s.format(g.getTime(; System.out.println(d;Calendar calendar = new GregorianCalendar(; calendar.setTime(date; calendar.add(calendar.DATE,1;/把日期往后增加一天.整數(shù)往后推,負(fù)數(shù)往前移動(dòng) date

11、=calendar.getTime(; Calendar c = Calendar.getInstance(; /得到當(dāng)前日期和時(shí)間c.set(Calendar.HOUR, 0; /把當(dāng)前時(shí)間小時(shí)變成c.set(Calendar.MINUTE, 0; /把當(dāng)前時(shí)間分鐘變成c.set(Calendar.SECOND, 0; /把當(dāng)前時(shí)間秒數(shù)變成c.set(Calendar.MILLISECOND, 0; /把當(dāng)前時(shí)間毫秒變成Date date1 = c.getTime(; /創(chuàng)建當(dāng)天的0時(shí)0分0秒一個(gè)date對(duì)象c.setFirstDayOfWeek(Calendar.SUNDAY; /設(shè)定星期

12、的第一天是星期天c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY; /把日期變成本周的星期天Date date2 = c.getTime(; /得到這個(gè)星期的星期天0時(shí)0分0秒c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY; /把日期變成本周的星期六Date date3 = c.getTime(; /得到這個(gè)星期的星期六0時(shí)0分0秒System.out.println(date1;System.out.println(date2;System.out.println(date3;當(dāng)天0點(diǎn)0分0秒 Calendar calendar=Calendar.getInstance(; calendar.set(Calendar.HOUR_OF_DAY,0; calendar.set(Calendar.MINUTE,0; calendar.set(Calendar.SECOND,0; calendar.set(Calendar.MILLISECOND,0; Date today = calendar.getTime(; 這個(gè)星期的星期天0時(shí)0分0秒和星期六0時(shí)0分0秒 接著上面的 c.add(Calendar.DATE,

溫馨提示

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

評(píng)論

0/150

提交評(píng)論