Gets month (specific month) between the two dates is a head and tail contains a head and tail not included

Internet and more recently to do a special project to calculate the difference between the even months of the month is to calculate the difference between the number of months that a difference of a few months will not put out a Baidu after the expiry of all the details of every month is the month difference For example, the calculation does not include the beginning and end of the month is the difference 2018-12-12-2019-03-31 2019-0102 "did not calculate the 201812 and 201903 month following code:

Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.set(2014, 9, 30);
        c2.setTime(new

                Date());
        System.out.println(c1.get(Calendar.YEAR) + " " + c1.get(Calendar.MONTH));
        while (c1.before(c2))

        {
            int m = c1.get(Calendar.MONTH) + 1;
            System.out.println(c1.get(Calendar.YEAR) + " " + (c1.get(Calendar.MONTH) + 1));
            c1.add(Calendar.MONTH, 1);
        }
        System.out.println(c1.get(Calendar.YEAR) + " " + (c1.get(Calendar.MONTH) + 1));

This seems to be first and second outputs are included, but if you enter the head and tail of the same date, the ideal situation needs to output only a month on the line but the actual output will be both a 

All have the following code:

Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        c1.set(2016, 12, 1);
        c2.set(2019, 12, 30);
        String time = "";
        String time2 = "";
        Integer i = c1.get(Calendar.MONTH);

        StringBuffer month = new StringBuffer("0");
        StringBuffer mon = new StringBuffer("0");
        if (i == 0) {
            i = 12;
            time = String.valueOf(c1.get(Calendar.YEAR) - 1) + "" + i;
        } else if (String.valueOf(i).length() < 2) {
            month.append(i + "");
            time = String.valueOf(c1.get(Calendar.YEAR)) + month + "";
        } else {
            time = String.valueOf(c1.get(Calendar.YEAR)) + "" + String.valueOf(c1.get(Calendar.MONTH));
        }
        if (String.valueOf(c2.get(Calendar.MONTH)).length() < 2) {
            mon.append(c2.get(Calendar.MONTH));
            time2 = String.valueOf(c2.get(Calendar.YEAR)) + mon + "";
        } else {
            time2 = String.valueOf(c2.get(Calendar.YEAR)) + "" + String.valueOf(c2.get(Calendar.MONTH));
        }
        while (Integer.valueOf(time2) >= Integer.valueOf(time)) {
            System.out.println(time);
            month = new StringBuffer("0");
            mon = new StringBuffer("0");
            i = c1.get(Calendar.MONTH) + 1;
            if (String.valueOf(i).length() < 2) {
                month.append(i + "");
                time = String.valueOf(c1.get(Calendar.YEAR)) + month + "";
            } else {
                time = String.valueOf(c1.get(Calendar.YEAR)) + "" + String.valueOf(c1.get(Calendar.MONTH) + 1);
            }
            if (String.valueOf(c2.get(Calendar.MONTH)).length() < 2) {
                mon.append(c2.get(Calendar.MONTH));
                time2 = String.valueOf(c2.get(Calendar.YEAR)) + mon + "";
            } else {
                time2 = String.valueOf(c2.get(Calendar.YEAR)) + "" + String.valueOf(c2.get(Calendar.MONTH));
            }
            c1.add(Calendar.MONTH, 1);
        }
        
    }

Many seem stupid but I want this resolved data if there is a better way, please exhibitions

Guess you like

Origin www.cnblogs.com/blackCatFish/p/11785876.html