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
可見,roll()方法在本月內(nèi)循環(huán),一般使用add()方法;
【Java認(rèn)證考試知識(shí)點(diǎn):Java時(shí)間類的函數(shù)】相關(guān)文章:
SUN認(rèn)證考試知識(shí)點(diǎn):java讀取文件內(nèi)容的函數(shù)09-27
java認(rèn)證考試介紹10-03
JAVA認(rèn)證考試細(xì)則10-03
Sun java認(rèn)證考試答案11-12
Sun Java認(rèn)證考試科目10-03
sun java認(rèn)證考試介紹10-03