天天看點

java 擷取目前日期的下一月的目前日期

1、目前日期的下一月的目前日期

private static String getLastMonthDate(String dataTime) throws ParseException {
        //時間字元串轉 LocalDate 類型
        LocalDate today = LocalDate.parse(dataTime);
        //目前月份+(-1)
        today = today.minusMonths(-1);
        //LocalDate日期格式是"YYYY-MM-DD",隻需要用toString()就可以轉化成字元串類型
        return today.toString();

    }      

2、另外一種方式

public static final SimpleDateFormat SHORT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");   
private static String getLastMonthDate(String repeatDate) {
        Calendar calendar = Calendar.getInstance();
        try {
            if(repeatDate!=null && !"".equals(repeatDate)){
                calendar.setTime(SHORT_DATE_FORMAT.parse(repeatDate));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)+1,calendar.get(Calendar.MINUTE));
        return SHORT_DATE_FORMAT.format(calendar.getTime());
}