Java gets the first day and last day of the month, gets the first and last day of the week

Source: Internet
Author: User
Tags dateformat

    /** * Gets the first and last day of the week of the specified date, with an underscore connection * @param datastr * @return * @throws parseexception*/     Public Staticstring Getfirstandlastofmonth (String datastr,string dateformat,string Resultdateformat) throws ParseException { //get the first day of the current month:Calendar C =calendar.getinstance (); C.settime (NewSimpleDateFormat (DateFormat). Parse (DATASTR)); C.add (Calendar.month,0); C.Set(Calendar.day_of_month,1);//set to number 1th, the current date is the first day of the monthString first =NewSimpleDateFormat (Resultdateformat). Format (C.gettime ()); System. out. println ("===============first:"+First ); //get the last day of the current monthCalendar CA =calendar.getinstance (); ca.Set(Calendar.day_of_month, Ca.getactualmaximum (Calendar.day_of_month)); String Last=NewSimpleDateFormat (Resultdateformat). Format (Ca.gettime ()); System. out. println ("===============last:"+Last ); returnfirst+"_"+Last ; }    /** * First and last day of the week * @param datastr * @param dateformat * @param resultdateformat * @return * @th Rows ParseException*/     Public Staticstring Getfirstandlastofweek (String datastr,string dateformat,string Resultdateformat) throws ParseException { Calendar Cal=calendar.getinstance (); Cal.settime (NewSimpleDateFormat (DateFormat). Parse (DATASTR)); intD =0; if(Cal.Get(calendar.day_of_week) = =1) {D= -6; } Else{d=2-Cal.Get(Calendar.day_of_week);        } cal.add (Calendar.day_of_week, D); //Week Start dateString data1 =NewSimpleDateFormat (Resultdateformat). Format (Cal.gettime ()); Cal.add (Calendar.day_of_week,6); //Week End DateString data2 =NewSimpleDateFormat (Resultdateformat). Format (Cal.gettime ()); returnData1 +"_"+data2; }

Java gets the first day and last day of the month, gets the first and last day of the week

Use the scene, calculate the day active, week active, month active is the date calculation

Java gets the first day and last day of the month, gets the first and last day of the week

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.