Date-related operations in Java went to the conference last night and came back at 12 o'clock. It took a long time to write this short piece of code today ..
Code:
/** <Br/> * @ Param date <br/> * @ return which month is belonged to the param date <br/> * @ author Xing, xiudong <br/> */<br/> Public int getmonthofdate (date) {<br/> calendar c = calendar. getinstance (); <br/> C. settime (date); <br/> int year = C. get (calendar. year); <br/> int month = C. get (calendar. month); <br/> date firstdayofnextmonth = datemedils. getfirstsatudayofmonth (<br/> month = 11? + Year: year, month = 11? 0: + + month); <br/> date lastdayofmonth = dateutils. adddays (firstdayofnextmonth,-1); <br/> C. settime (date); <br/> int result = C. get (calendar. month); <br/> return date. after (lastdayofmonth )? ++ Result: result; <br/>}</P> <p>/** <br/> * @ Param date <br/> * @ return which year is belonged the param date <br/> * @ author Xing, xiudong <br/> */<br/> Public int getyearofdate (date) {<br/> calendar c = calendar. getinstance (); <br/> C. settime (date); <br/> int year = C. get (calendar. year); <br/> C. set (+ + year, 1, 1); <br/> date firstdayofyear = datetools. getfirstsatudayofmonth (C <br/>. ge T (calendar. Year), 0); <br/> int result = C. Get (calendar. year); <br/> return date. Before (firstdayofyear )? -- Result: result; <br/>}
Add the previous Code:
/** <Br/> * @ Param date <br/> * @ return first day (satuday) of month <br/> * @ author Jin, qinghua <br/> */<br/> Public static date getfirstsatudayofmonth (INT year, int month) {<br/> calendar Cal = gregoriancalendar. getinstance (); <br/> Cal. set (year, month, 1); </P> <p> Cal. setfirstdayofweek (calendar. saturday); <br/> Cal. set (calendar. day_of_week, Cal. getfirstdayofweek (); <br/> return Cal. gettime (); <br/>}</P> <p>/** <br/> * @ Param date <br/> * @ return first day (satuday) of week <br/> * @ author Wu, Yang <br/> */<br/> Public static date getfirstsatudayofweek (date) {<br/> calendar Cal = gregoriancalendar. getinstance (); <br/> Cal. settime (date); <br/> Cal. setfirstdayofweek (calendar. saturday); <br/> Cal. set (calendar. day_of_week, Cal. getfirstdayofweek (); <br/> return Cal. gettime (); <br/>}</P> <p>/** <br/> * @ Param date <br/> * @ return week count of month date <br/> * @ author Xing, xiudong <br/> */<br/> Public integer getweekcountofmonth (INT year, int month) {<br/> date first_day_month = datw.ls <br/>. getfirstsatudayofmonth (year, month-1); </P> <p> calendar cal_lastly = calendar. getinstance (); <br/> cal_lastly.set (year, month, 1); <br/> date firstdayofnextmonth = cal_lastly.gettime (); </P> <p> integer COUNT = 0; <br/> for (date first_day_week = first_day_month; first_day_week <br/>. before (firstdayofnextmonth); first_day_week = dateutils <br/>. adddays (first_day_week, 7) {<br/> count ++; <br/>}</P> <p> return count; <br/>}