Use the Java Calendar object to get the start and end times of the current date

Source: Internet
Author: User

Ideas:

Get the start and end date of the current quarter first, pushing forward 3 months on the current date, i.e. the start and end dates of the previous quarter

/** * @param flag true: Start date, false: End date * @return */public static String Getlastquartertime (Boolean flag) {SimpleDateFormat SHORTSDF = new SimpleDateFormat ("Yyyy-mm-dd"); SimpleDateFormat longsdf = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String resultdate= "";D ate now = null;try {Calendar calendar = calendar.getinstance (); int currentmonth = Calendar.get (Cale Ndar. MONTH) + 1;//true: Start date, false: End date if (flag) {if (currentmonth >= 1 && currentmonth <= 3) Calendar.set ( Calendar.month, 0), else if (currentmonth >= 4 && currentmonth <= 6) Calendar.set (Calendar.month, 3); else if ( Currentmonth >= 7 && currentmonth <= 9) Calendar.set (Calendar.month, 6); else if (Currentmonth >= ten && Amp Currentmonth <=) Calendar.set (Calendar.month, 9); Calendar.set (calendar.date, 1); now = Longsdf.parse ( Shortsdf.format (Calendar.gettime ()) + "00:00:00"); Else{if (currentmonth >= 1 && currentmonth <= 3) {Calendar.set (Calendar.month, 2); Calendar.set (Calendar.            DATE, 31);            } else if (Currentmonth >= 4 && currentmonth <= 6) {Calendar.set (Calendar.month, 5);            Calendar.set (Calendar.date, 30);            } else if (currentmonth >= 7 && currentmonth <= 9) {Calendar.set (calendar.month, 8);            Calendar.set (Calendar.date, 30);            } else if (currentmonth >= && currentmonth <=) {calendar.set (calendar.month, 11);            Calendar.set (Calendar.date, 31); }now = Longsdf.parse (Shortsdf.format (Calendar.gettime ()) + "23:59:59");} Calendar.settime (now);//Set Date Calendar.add (Calendar.month,-3); resultdate = Longsdf.format (Calendar.gettime ());} catch (Exception e) {;} return resultdate;}

  

Use the Java Calendar object to get the start and end times of the current 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.