- 相關(guān)推薦
Java認(rèn)證考試知識(shí)點(diǎn):Java時(shí)間類的函數(shù)
學(xué)習(xí)是一條慢長(zhǎng)而艱苦的道路,不能靠一時(shí)激情,也不是熬幾天幾夜就能學(xué)好的,必須養(yǎng)成平時(shí)努力學(xué)習(xí)的習(xí)慣,Java也不例外,下面小編為大家整理了Java里時(shí)間類以及函數(shù)的相關(guān)文章:
1Calendar和Date的轉(zhuǎn)化
(1)Calendar轉(zhuǎn)化為Date
Calendar cal=Calendar.getInstance();
Date date=cal.getTime();
(2)Date轉(zhuǎn)化為Calendar
Date date=new Date();
Calendar cal=Calendar.getInstance();
cal.setTime(date);
2.計(jì)算某一月份的最大天數(shù)
Calendar time=Calendar.getInstance();
time.clear();
time.set(Calendar.YEAR,year);
time.set(Calendar.MONTH,i-1);//注意,Calendar對(duì)象默認(rèn)一月為0
int day=time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天數(shù)
注:在使用set方法之前,必須先clear一下,否則很多信息會(huì)繼承自系統(tǒng)當(dāng)前時(shí)間
3.格式化輸出日期時(shí)間
Date date=new Date();
SimpleDateFormat df=new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”);
System.out.println(df.format(date));
4.計(jì)算一年中的第幾星期
(1)計(jì)算某一天是一年中的第幾星期
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR, 2006);
cal.set(Calendar.MONTH,;
cal.set(Calendar.DAY_OF_MONTH, 3);
int weekno=cal.get(Calendar.WEEK_OF_YEAR);
(2)計(jì)算一年中的第幾星期是幾號(hào)
SimpleDateFormat df=new SimpleDateFormat(“yyyy-MM-dd”);
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR, 2006);
cal.set(Calendar.WEEK_OF_YEAR, 1);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
System.out.println(df.format(cal.getTime()));
輸出:
2006-01-02
5.add()和roll()的用法
(1)add()方法
SimpleDateFormat df=new SimpleDateFormat(“yyyy-MM-dd”);
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR, 2006);
cal.set(Calendar.MONTH,;
cal.set(Calendar.DAY_OF_MONTH, 3);
cal.add(Calendar.DATE, -4);
Date date=cal.getTime();
System.out.println(df.format(date));
cal.add(Calendar.DATE, 4);
date=cal.getTime();
System.out.println(df.format(date));
輸出:
2006-08-30
2006-09-03
(2)roll方法
cal.set(Calendar.YEAR, 2006);
cal.set(Calendar.MONTH,;
cal.set(Calendar.DAY_OF_MONTH, 3);
cal.roll(Calendar.DATE, -4);
date=cal.getTime();
System.out.println(df.format(date));
cal.roll(Calendar.DATE, 4);
date=cal.getTime();
System.out.println(df.format(date));
輸出:
2006-09-29
2006-09-03
可見(jiàn),roll()方法在本月內(nèi)循環(huán),一般使用add()方法;
【Java認(rèn)證考試知識(shí)點(diǎn):Java時(shí)間類的函數(shù)】相關(guān)文章:
JAVA認(rèn)證考試細(xì)則06-10
java認(rèn)證考試介紹07-31
sun java認(rèn)證考試介紹10-23
JAVA認(rèn)證考試費(fèi)用06-25
Sun java認(rèn)證考試答案11-06
JAVA考試認(rèn)證經(jīng)驗(yàn)分享08-22
Sun Java認(rèn)證考試科目08-30