传参类型 String
yyyy-MM
public String lastDate (String date){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); String lastDate = ""; try { Date currentDate = sdf.parse(date); Calendar calendar = Calendar.getInstance(); calendar.setTime(currentDate); calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); lastDate = sdf.format(calendar.getTime()); } catch (Exception e){ e.printStackTrace(); } return lastDate; }
yyyy-MM-dd
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd"); String lastDate = LocalDate.parse(startDate, fmt).minusMonths(1).format(fmt);