import java.text.SimpleDateFormat;
import java.util.Calendar;
public class test {
private static String startDay = "2020-02-20";
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(Integer.parseInt(startDay.substring(0,4)), Integer.parseInt(startDay.substring(5,7)) - 1, 1);
String firstDayOfMonth = new SimpleDateFormat( "yyyy-MM-dd ").format(calendar.getTime());
System.out.println("第一天:"+firstDayOfMonth);
calendar.set(Integer.parseInt(startDay.substring(0,4)), Integer.parseInt(startDay.substring(5,7)), 1);//这里先设置要获取月份的下月的第一天
calendar.add(Calendar.DATE, -1);//这里将日期值减去一天,从而获取到要求的月份最后一天
String lastDayOfMonth = new SimpleDateFormat( "yyyy-MM-dd ").format(calendar.getTime());
System.out.println("最后一天:"+lastDayOfMonth);
}
}
参考博客:https://blog.csdn.net/Xiehou_java/article/details/87937749
感谢大佬!!!
.end