Get the starting and ending dates for a week

Source: Internet
Author: User

private static final SimpleDateFormat format_date = new SimpleDateFormat (
Confighomehelper.getvaluebyconfig ("Format.date"));

/**
  * Calculates the start date of a week for a year
  *
  * @param yearnum
  *             format yyyy
  * @param weekNum
  *             1 to 52 or
  * @return date in YYYY-MM-DD
  */
 public static String getyearweekfirstday (int yearno, int weekno) {
  calendar cal = Calendar.getinstance ();
 & Nbsp;cal.setfirstdayofweek (Calendar.sunday); Set the first day of the week for the Monday
  cal.set (Calendar.day_of_week, calendar.sunday);//weekly starting from Monday
  // The above two lines of code can be implemented, the first week of each year, is the week that contains the first Monday.
  cal.setminimaldaysinfirstweek (7);//Set minimum 7 days per week
  cal.set (calendar.year, Yearno);
  cal.set (Calendar.week_of_year, Weekno);
  //The year, month, day
  return Format_date.format (Cal.gettime ()) of the current date respectively;
 }

 /**
  * Calculates the end date of a week for a year
  *
  * @param yearnum
  *             format yyyy
  * @param weekNum
  *             1 to 52 or
  * @return date in YYYY-MM-DD
  */
 public static String getyearweekendday (int yearno, int weekno) {
  calendar cal = Calendar.getinstance ();
&nbs P; cal.setfirstdayofweek (Calendar.sunday); Set the first day of the week for Monday
  cal.set (calendar.day_of_week,calendar.saturday);//weekly from Monday
  // The above two lines of code can be implemented, the first week of each year, is the week that contains the first Monday.
  cal.setminimaldaysinfirstweek (7);//Set minimum 7 days per week
  cal.set (calendar.year, Yearno);
  cal.set (Calendar.week_of_year, Weekno);
  return Format_date.format (Cal.gettime ());
 }

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.