JAVA--获取当前日期两个月之前的日期

public static void main(String[] args){
		try{
			String nowTime="2019-05-31 10:29:00";
			String timeBefore=getTwoMonthBefore(nowTime);
			System.out.println("您当前传入时间:"+nowTime+"的两个月之前的时间为:"+timeBefore);
		}catch(Exception e){e.printStackTrace();}
	}
	
	// 获取当前日期2个月前的时间
	public static String getTwoMonthBefore(String nowTime)throws ParseException{

        // 获取当前时间
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=dateFormat.parse(nowTime);

        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(date);//把当前时间赋给日历
        calendar.add(calendar.MONTH, -2); //设置为前2月,可根据需求进行修改
        date = calendar.getTime();//获取2个月前的时间

        return dateFormat.format(date);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值