Java gets past 12 month date __localdate

Source: Internet
Author: User

If you have a demand, check the past 12 months of the tax trend chart, do not recommend you in the SQL Mosaic returned 12 months of data, it is likely to lead to slow SQL, it is recommended in the Java code to get the date, and then passed into the SQL based on the date to query. Get the month of the past 12 months as follows:

    public static void Main (string[] args) {
        localdate today = Localdate.now ();
        for (Long i = 1l;i <= 12L; i++) {
            localdate localdate = today.minusmonths (i);
            String ss = localdate.tostring (). substring (0,7);
            System.out.println (localdate);
            SYSTEM.OUT.PRINTLN (ss);
            System.out.println ("==============");
        }
    

Print as follows:

2018-03-18 2018-03 ============== 2018-02-18 2018-02 ============== 2018-01-18 2018-01 = =
======== 2017-12-18 2017-12 ============== 2017-11-18 2017-11 ============== 2017-10-18 2017-10 ==============
2017-09-18 2017-09 ============== 2017-08-18 2017-08 ============== 2017-07-18 2017-07 ============== 2017-06-18 2017-06 ============== 2017-05-18 2017-05 ============== 2017-04-18 2017-04 

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.