Java obtains the first day of a week in a year.

Source: Internet
Author: User

Directly run the Code [java] import java. util. calendar; import java. util. date; import org. apache. commons. logging. log; import org. apache. commons. logging. logFactory;/*** date tool class * @ author WXQ **/public class DateUtils {private static final Log log = LogFactory. getLog (DateUtils. class);/*** obtain the week of the current date ** @ 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);}/*** get the total number of weeks in a year ** @ param year * @ return */public static int getMaxWeekNumOfYear (int year) {Calendar Ar c = Calendar ar. 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 of a year ** @ 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 of a year ** @ 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 (); log.info (getLastDayOfWeek (); log.info (getFirstDayOfMonth )); log.info (getLastDayOfMonth (2013); log.info (getFirstDayOfQuarter (); log.info (getLastDayOfQuarter (); log.info (getFirstDayOfYear )); log.info (getLastDayOfYear (2013 ));}}

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.