Pass in year and month, concatenate day

Calendar cal = Calendar.getInstance();
if (datetype.equals("month")) {
            startTime= startTime+"-01";
            int year = Integer.parseInt(endTime.split("-")[0]);
            int month = Integer.parseInt(endTime.split("-")[1]);
            cal.set(year,month,1);    //1为1日
            //java月份从0开始,输入的月份比实际得到的月+1,即month值+1月1日
            //如,输入的是3月,输出的为4月
            cal.add(Calendar.DATE,-1);//-1为减1天,即month值+1月1的前一天,此时可得到想要的正确的月份
            int day = cal.get(Calendar.DATE);
            endTime= year +"-"+month+"-"+day;
}

As shown in the figure above, the start time is startTime and the end time is endTime.

Automatically concatenates the first day of the start month and the last day of the end month.

Guess you like

Origin blog.csdn.net/guapilixianghe/article/details/128661115
Recommended