Java gets the implementation idea and Code on the first day of a week in a year, and the java idea

Source: Internet
Author: User

Java gets the implementation idea and Code on the first day of a week in a year, and the java idea
Directly Add code
Copy codeThe Code is as follows:
Import java. util. Calendar;
Import java. util. Date;
Import org. apache. commons. logging. Log;
Import org. apache. commons. logging. LogFactory;
/**
* Date Tool
* @ Author WXQ
*
*/
Public class DateUtils {

Private static final Log log = LogFactory. getLog (DateUtils. class );
/**
* How many weeks is the current date obtained?
*
* @ Param date
* @ Return
*/
Public static int getWeekOfYear (Date date ){
Calendar c = Calendar. getInstance ();
C. setFirstDayOfWeek (Calendar. MONDAY );
/** Set the minimum number of days required for the first week of the year. For example, if the first week contains the first day of the first month of the year, use value 1 to call this method.
* If the minimum number of days must be one week, call this method with a value of 7. **/
C. setMinimalDaysInFirstWeek (1 );
C. setTime (date );
Return c. get (Calendar. WEEK_OF_YEAR );
}
/**
* Obtain the total number of weeks in a year.
*
* @ Param year
* @ Return
*/
Public static int getMaxWeekNumOfYear (int year ){
Calendar c = Calendar. getInstance ();
C. set (year, Calendar. DECEMBER, 31, 23, 59, 59 );
Return getWeekOfYear (c. getTime ());
}
/**
* Get the first day of a week of a year.
*
* @ Param year
* @ Param week
* @ Return
*/
Public static Date getFirstDayOfWeek (int year, int week ){
Calendar c = Calendar. getInstance ();
C. set (Calendar. YEAR, year );
C. set (Calendar. WEEK_OF_YEAR, week );
C. set (Calendar. DAY_OF_WEEK, Calendar. MONDAY); // set MONDAY
C. setFirstDayOfWeek (Calendar. MONDAY );
Return c. getTime ();
}

/**
* Get the last day of a week of a year.
*
* @ Param year
* @ Param week
* @ Return
*/
Public static Date getLastDayOfWeek (int year, int week ){
Calendar c = Calendar. getInstance ();
C. set (Calendar. YEAR, year );
C. set (Calendar. WEEK_OF_YEAR, week );
C. setFirstDayOfWeek (Calendar. MONDAY );
C. set (Calendar. DAY_OF_WEEK, c. getFirstDayOfWeek () + 6); // Sunday

Return c. getTime ();
}

/**
* Get the first day of a month.
*
* @ Param year
* @ Param month
* @ Return
*/
Public static Date getFirstDayOfMonth (int year, int month ){
Calendar c = Calendar. getInstance ();
C. set (Calendar. YEAR, year );
C. set (Calendar. MONTH, month-1 );
C. set (Calendar. DAY_OF_MONTH, c. getActualMinimum (Calendar. DAY_OF_MONTH ));
Return c. getTime ();
}

/**
* Get the last day of a month
*
* @ Param year
* @ Param month
* @ Return
*/
Public static Date getLastDayOfMonth (int year, int month ){
Calendar c = Calendar. getInstance ();
C. set (Calendar. YEAR, year );
C. set (Calendar. MONTH, month-1 );
C. set (Calendar. DAY_OF_MONTH, c. getActualMaximum (Calendar. DAY_OF_MONTH ));
Return c. getTime ();
}
/**
* Get the first day of a certain quarter of a year
*
* @ Param year
* @ Param quarter
* @ Return
*/
Public static Date getFirstDayOfQuarter (int year, int quarter ){
Int month = 0;
If (quarter> 4 ){
Return null;
} Else {
Month = (quarter-1) * 3 + 1;
}
Return getFirstDayOfMonth (year, month );
}
/**
* Get the last day of a quarter of a year
*
* @ Param year
* @ Param quarter
* @ Return
*/
Public static Date getLastDayOfQuarter (int year, int quarter ){
Int month = 0;
If (quarter> 4 ){
Return null;
} Else {
Month = quarter * 3;
}
Return getLastDayOfMonth (year, month );
}
/**
* Get the first day of a year
*
* @ Param year
* @ Return
*/
Public static Date getFirstDayOfYear (int year ){
Return getFirstDayOfQuarter (year, 1 );
}

/**
* Get the last day of a year
*
* @ Param year
* @ Return
*/
Public static Date getLastDayOfYear (int year ){
Return getLastDayOfQuarter (year, 4 );
}

Public static void main (String [] args ){
Log.info (getFirstDayOfWeek (2011,1 ));
Log.info (getLastDayOfWeek (2011,1 ));

Log.info (getFirstDayOfMonth (2011,2 ));
Log.info (getLastDayOfMonth (2011,2 ));

Log.info (getFirstDayOfQuarter (2011,2 ));
Log.info (getLastDayOfQuarter (2011,2 ));

Log.info (getFirstDayOfYear (2013 ));
Log.info (getLastDayOfYear (2013 ));

}
}

Related Article

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.