当前位置: 代码迷 >> 综合 >> Java中时间的加减运算~
  详细解决方案

Java中时间的加减运算~

热度:96   发布时间:2024-01-26 14:16:50.0

java中的时间运算的方法有多种,我觉得最好用的是这一种:

 

Date now2 = new Date();//获取当前时间
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(now2);//将当前时间设置进去
根据需要去加减时间
rightNow.add(Calendar.MONTH,-1);//日期减去1个月
rightNow.add(Calendar.DAY,-1);//日期减去1天
Date now2Result=rightNow.getTime();//最后的时间
格式化一下
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String resultDate6=sdf2.format(now2Result);

ok。输出resultDate6就是你要的时间了