JAVA获取当前日期指定月份后(多少个月后)的日期
环境要求:使用jdk1.8
package com.date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; public class Test1 { public static void main(String[] args) throws ParseException { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date=getMonthDate(sdf.parse("2020-01-11 11:22:33"),1); System.out.println("后一个月的时间:"+sdf.format(date)); } /** * 获取startDate日期后month月的日期 * @param startDate 开始日期 * @param month 几个月后 * @return */ public static Date getMonthDate(Date startDate,int month){ LocalDateTime localDateTime = startDate.toInstant() .atZone(ZoneId.systemDefault() ) .toLocalDateTime().plusMonths(month); Date date = Date.from(localDateTime.atZone( ZoneId.systemDefault()).toInstant()); return date; } }
-----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------
(蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)