Use the Calendar Object of java to obtain the last few degrees of start and end time of the current date, javacalendar

Source: Internet
Author: User

Use the Calendar Object of java to obtain the last few degrees of start and end time of the current date, javacalendar

Ideas:

Obtain the start and end dates of the current quarter, and push the start and end dates of the previous quarter three months forward based on the current date.

/*** @ Param flag true: Start date; false: end date * @ return */public static String getLastQuarterTime (boolean flag) {SimpleDateFormat required SDF = new SimpleDateFormat ("yyyy-MM-dd"); SimpleDateFormat longSdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "); string resultDate = ""; Date now = null; try {Calendar calendar = Calendar. getInstance (); int currentMonth = calendar. get (Calendar. 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> = 10 & currentMonth <= 12) calendar. set (Calendar. MONTH, 9); calendar. set (Calendar. DATE, 1); now = longSdf. parse (fig. 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 >=10 & currentMonth <= 12) {calendar ar. set (Calendar. MONTH, 11); calendar. set (Calendar. DATE, 31);} now = longSdf. parse (fig. format (calendar. getTime () + "23:59:59");} calendar. setTime (now); // set the date calendar. add (Calendar. MONTH,-3); resultDate = longSdf. format (calendar. getTime ();} catch (Exception e) {;} return resultDate ;}

  

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.