java获取三个月之前时间与当前时间


Date dNow = new Date(); //当前时间
Date dBefore = new Date();
Calendar calendar = Calendar.getInstance(); //得到日历
calendar.setTime(dNow);//把当前时间赋给日历
calendar.add(Calendar.MONTH, -3); //设置为前3月
dBefore = calendar.getTime(); //得到前3月的时间
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
String defaultStartDate = sdf.format(dBefore); //格式化前3月的时间
String defaultEndDate = sdf.format(dNow); //格式化当前时间
System.out.println("三个月之前时间======="+defaultStartDate);
System.out.println("当前时间==========="+defaultEndDate);


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM