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 ..

Source: Internet
Author: User

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/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.