资源描述:
《常用java date方法集合(常用java date方法集合)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、常用javadate方法集合(常用javadate方法集合)1.calculatethemaximumnumberofdaysinJanuaryCalendar,time=Calendar.getInstance();Time.clear();Time.set(Calendar.YEAR,year);//yearisintTime.set(Calendar.MONTH,i-1);//note,Calendardefaultis0JanuaryIntday=time.getActualMaximum(Calendar.DAY_OF_MONTH
2、);//numberofdaysthismonth.Note:beforeusingthesetmethod,youmustfirstclear,otherwisemanymessageswillinheritfromthecurrentsystemtimeTransformationof2.CalendarandDate(1)CalendarisconvertedtoDateCalendar,cal=Calendar.getInstance();Date,date=cal.getTime();(2)DateisconvertedtoCal
3、endarDate,date=new,Date();Calendar,cal=Calendar.getInstance();Cal.setTime(date);3.formattheoutputdateandtime(thisisusedmore)Date,date=new,Date();SimpleDateFormatdf=newSimpleDateFormat("yyyy-MM-ddhh:mm:ss");Stringtime=df.format(date);System.out.println(time);4.calculatethef
4、irstfewweeksoftheyear(1)calculatewhatdayaweekisinayearCalendar,cal=Calendar.getInstance();Cal.set(Calendar.YEAR,2006);Cal.set(Calendar.MONTH,8);Cal.set(Calendar.DAY_OF_MONTH,3);Intweekno=cal.get(Calendar.WEEK_OF_YEAR);(2)whatisthefirstweekoftheyear?SimpleDateFormat,df=new,
5、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()));Output:2006-01-02Theuseof5.add()androll()isnotverycomm
6、on(1)add()methodSimpleDateFormat,df=new,SimpleDateFormat("yyyy-MM-dd");Calendar,cal=Calendar.getInstance();Cal.set(Calendar.YEAR,2006);Cal.set(Calendar.MONTH,8);Cal.set(Calendar.DAY_OF_MONTH,3);Cal.add(Calendar.DATE,-4);Date,date=cal.getTime();System.out.println(df.format(
7、date));Cal.add(Calendar.DATE,4);Date=cal.getTime();System.out.println(df.format(date));Output:2006-08-302006-09-03(2)rollmethodCal.set(Calendar.YEAR,2006);Cal.set(Calendar.MONTH,8);Cal.set(Calendar.DAY_OF_MONTH,3);Cal.roll(Calendar.DATE,-4);Date=cal.getTime();System.out.pr
8、intln(df.format(date));Cal.roll(Calendar.DATE,4);Date=cal.getTime();System.out.println(df