Java calculates the Monday and Sunday of the week based on the specified date

Source: Internet
Author: User

Public static void main (String[] args)  throws ParseException {                     Simpledateformat sdf=new simpledateformat ("Yyyy-mm-dd");  //set time format             calendar cal = calendar.getinstance ();            date time=sdf.parse ("2015-9-4 14:22:47");          cal.settime (Time);            system.out.println ("To calculate Date:" +sdf.format (Cal.gettime ()))  //output to calculate date                      // Determine whether the date to be calculated is Sunday, if it is minus one day to calculate the Saturday, otherwise there will be a problem, calculated to the next week             Int dayweek = cal.gET (Calendar.day_of_week);//Get the current date is the first day of the one weeks            if (1 == dayweek)  {               cal.add (calendar.day_of_month, -1);            }                    cal.setfirstdayofweek (calendar.monday);//Set one the first day of the week, according to the Chinese habit one weeks the first day is Monday                    int day =  Cal.get (Calendar.day_of_week);//Get the current date is the day of the one week            Cal.add (Calendar.date, cal.getfirstdayofweek ()-day);//According to the rules of the calendar, subtract the difference between the day of the week and the first of the one weeks of the current date             system.out.println ("Date of week Monday:" +sdf.format (Cal.getTime ()));      &Nbsp;  system.out.println (Cal.getfirstdayofweek () + "-" +day+ "+6=" + (Cal.getfirstdayofweek ()-day+6));                 cal.add ( CALENDAR.DATE, 6);         system.out.println ("Date of week Sunday:" + Sdf.format (Cal.gettime ()));                }


Java calculates the Monday and Sunday of the week based on the specified date

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.