Java returns data of the form 2015 3/20-3/31 for a given date

Source: Internet
Author: User

One of the most recent projects has a foreground for the table header requirements:

Returns the shape of a given date period as

Mar 2015 3/20-3/31
APR 2015 4/1-4/30

This month year data, simply write down the code, temporarily did not think of a better way

For example, the passed in parameter is "2015-03-20"-"2016-04-08" such form

Three-stage processing:

First paragraph: The end of the start date of the start date

Second paragraph: Data for all months between the start and end months

Third paragraph: End date of the beginning of the month to the end date

1  PublicList<string> loadmonthstr (String startdate, String endDate)throwsException {2List<string> periodlist =NewArraylist<>();3SimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd");4GregorianCalendar Cdstart =NewGregorianCalendar ();5GregorianCalendar Cdend =NewGregorianCalendar ();6 Cdstart.settime (Sdf.parse (StartDate));7 Cdend.settime (Sdf.parse (endDate));8         9SDF =NewSimpleDateFormat ("MMM yyyy", locale.us);Ten         intFirststart =Cdstart.get (calendar.day_of_month); One          A         intLastend =Cdend.get (calendar.day_of_month); -         intMonthofyear = Cdstart.get (calendar.month) + 1; -          the         BooleanSameyearmonth =false; - String datestr; -         if(Cdstart.get (calendar.month) = = Cdend.get (calendar.month) && cdstart.get (calendar.year) = =Cdend.get (calendar.year)) { -Sameyearmonth =true; +         } -          +         if(sameyearmonth) { ADatestr = Sdf.format (Cdstart.gettime ()) + "" + Monthofyear + "/" + Firststart + "-" + Monthofyear + "/" +Lastend; at Periodlist.add (DATESTR); -}Else { -Datestr = Sdf.format (Cdstart.gettime ()) + "" + Monthofyear + "/" + cdstart.get (calendar.day_of_month) + "-" -+ Monthofyear + "/" +Cdstart.getmaximum (calendar.day_of_month); - Periodlist.add (DATESTR); -Cdstart.set (Calendar.day_of_month, 1); inCdend.set (Calendar.day_of_month, 1); -Cdstart.add (Calendar.month, 1); to             intStartDay = 0; +             intEndday = 0; -              while(Cdstart.compareto (Cdend) < 0) { theMonthofyear = Cdstart.get (calendar.month) + 1; *StartDay = 1; $Endday =Cdstart.getactualmaximum (calendar.day_of_month);Panax NotoginsengDatestr = Sdf.format (Cdstart.gettime ()) + "" + Monthofyear + "/" + StartDay + "-" + Monthofyear + "/" +Endday; -Cdstart.add (Calendar.month, 1); the Periodlist.add (DATESTR); +             } ADatestr = Sdf.format (Cdend.gettime ()) + "" + (Cdend.get (calendar.month) + 1) + "/" + 1 + "-" the+ (Cdend.get (calendar.month) + 1) + "/" +Lastend; + Periodlist.add (DATESTR); -         } $         returnperiodlist; $}

Java returns data of the form 2015 3/20-3/31 for a given 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.